Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用linq分离项目?_C#_Linq - Fatal编程技术网

C# 如何使用linq分离项目?

C# 如何使用linq分离项目?,c#,linq,C#,Linq,我有一个文章网站的数据集。每篇文章都有datetime,但我不能用Linq根据datetime将它们分开 比如这个 如您所见,数据集生成月份和年份 使用linq如何分离此数据集 我想如果我像这样使用列表也许我可以,但是这次失败 假设我有一门课: public class Article { public DateTime PostDate {get;set;} } 或带有PostDate列的数据行 以及这些物品的清单: List<Article> 列表 我想

我有一个文章网站的数据集。每篇文章都有datetime,但我不能用Linq根据datetime将它们分开

比如这个

如您所见,数据集生成月份和年份

使用linq如何分离此数据集

我想如果我像这样使用
列表
也许我可以,但是这次失败

假设我有一门课:

 public class Article
 {
      public DateTime PostDate {get;set;}
 }
或带有
PostDate
列的数据行

以及这些物品的清单:

 List<Article>
列表
我想根据使用Linq的
PostDate
将这些文章分为第一个
Year
和第二个
Month

这样我就可以按如下方式输出它们:

  • 2013年|文章发布日期.年份
    • 12月|文章.PostDate.Month
      • 文章标题A|文章标题
      • 第B条标题
    • 十一月
    • 十月
  • 2012年
    • 十二月
    • 十一月
    • 十月

如果我正确理解了您的问题,那么您希望能够先按年分组,然后按月分组

这里有一个方法:

public class Article
{
    public DateTime PostDate {get;set;}
}

void Main()
{
    DateTime now = DateTime.Now;
    //create some fake data!
    List<Article> articles = new List<Article>{
                                        new Article{PostDate=now}, 
                                        new Article{PostDate=now.AddMinutes(1)}, 
                                        new Article{PostDate=now.AddMonths(1)}, 
                                        new Article{PostDate=now.AddMonths(1).AddMinutes(1)}, 
                                        new Article{PostDate=now.AddMonths(2)}, 
                                        new Article{PostDate=now.AddMonths(2).AddMinutes(1)}, 
                                        new Article{PostDate=now.AddMonths(4)},
                                        new Article{PostDate=now.AddMonths(4).AddMinutes(1)}
                                };

    var groupedArticles = from a in articles
                        group a by a.PostDate.Year into articlesByYear
                            select new {
                                Year = articlesByYear.Key,
                                ArticlesByMonth = from a2 in articlesByYear
                                                group a2 by a2.PostDate.Month into articlesByYearByMonth    
                                                select articlesByYearByMonth
                            };


    foreach (var articlesByYear in groupedArticles)
    {
        foreach (var articlesByMonthForCurrentYear in articlesByYear.ArticlesByMonth)
        {
            foreach (var article in articlesByMonthForCurrentYear)
            {
                Console.WriteLine("Year: " + articlesByYear.Year +" Month: " + articlesByMonthForCurrentYear.Key + " PostDate: " + article.PostDate);
            }
        }
    }
}

我明白,你试图把文章按年分,然后按月分

假设您有以下文章类:

class Article
{
    public DateTime PostDate { get; set; }
    public string Title { get; set; }

    public Article(DateTime postDate, string title)
    {
        PostDate = postDate;
        Title = title;
    }
}
以及以下样本数据:

var list = new List<Article>
{
    new Article(new DateTime(2012, 12, 1), "A"),
    new Article(new DateTime(2012, 12, 1), "B"),
    new Article(new DateTime(2012, 11, 2), "C"),
    new Article(new DateTime(2012, 11, 2), "D"),
    new Article(new DateTime(2013, 12, 1), "E")
};
并按如下方式输出:

var grouped = list.GroupBy(a => a.PostDate.Year)
              .Select(a => new { Year = a.Key, Items = a.GroupBy(i => i.PostDate.Month)
                                                        .Select(i => new { Month = i.Key, Titles = i.Select(t => t.Title)})
                                                        .OrderBy(i => i.Month)})
              .OrderBy(a => a.Year);
foreach (var year in grouped)
{
    Console.WriteLine("Year: " + year.Year);
    foreach (var month in year.Items)
    {
        Console.WriteLine("  Month: " + month.Month);
        foreach (var title in month.Titles)
        {
            Console.WriteLine("    Title: " + title);
        }
    }
}
以下是输出:

Year: 2012
  Month: 11
    Title: C
    Title: D
  Month: 12
    Title: A
    Title: B
Year: 2013
  Month: 12
    Title: E

一个稍微简单的查询,具有相同的结果:

        var articles = new List<Article>(new[]
        {
            new Article(){Title="Article A", PostDate=DateTime.Today.AddMonths(-1)},
            new Article(){Title="Article A", PostDate=DateTime.Today.AddMonths(-2)},
            new Article(){Title="Article A", PostDate=DateTime.Today.AddMonths(-3)},
            new Article(){Title="Article A", PostDate=DateTime.Today.AddYears(-1)},
            new Article(){Title="Article A", PostDate=DateTime.Today.AddYears(-1).AddMonths(-1)},
            new Article(){Title="Article A", PostDate=DateTime.Today.AddYears(-1).AddMonths(-2)},
            new Article(){Title="Article A", PostDate=DateTime.Today.AddYears(-1).AddMonths(-3)},
        });

        var set = from article in articles
                  group article by new { article.PostDate.Year, article.PostDate.Month }
                  into byMonthYear
                  group byMonthYear by byMonthYear.Key.Year
                  into byYear
                  select byYear;
var articles=新列表(新[]
{
new Article(){Title=“Article A”,PostDate=DateTime.Today.AddMonths(-1)},
new Article(){Title=“Article A”,PostDate=DateTime.Today.AddMonths(-2)},
new Article(){Title=“Article A”,PostDate=DateTime.Today.AddMonths(-3)},
new Article(){Title=“Article A”,PostDate=DateTime.Today.AddYears(-1)},
new Article(){Title=“Article A”,PostDate=DateTime.Today.AddYears(-1).AddMonths(-1)},
new Article(){Title=“Article A”,PostDate=DateTime.Today.AddYears(-1).AddMonths(-2)},
new Article(){Title=“Article A”,PostDate=DateTime.Today.AddYears(-1).AddMonths(-3)},
});
var set=来自文章中的文章
按新{article.PostDate.Year,article.PostDate.Month}分组文章
进入拜蒙特耳
按月份分组按月份分组
进入拜年
选择byYear;

什么是“独立”?请举例说明您正在使用的源数据和您已经尝试过的源数据,以及您希望通过linq获得的预期数据结构。
        var articles = new List<Article>(new[]
        {
            new Article(){Title="Article A", PostDate=DateTime.Today.AddMonths(-1)},
            new Article(){Title="Article A", PostDate=DateTime.Today.AddMonths(-2)},
            new Article(){Title="Article A", PostDate=DateTime.Today.AddMonths(-3)},
            new Article(){Title="Article A", PostDate=DateTime.Today.AddYears(-1)},
            new Article(){Title="Article A", PostDate=DateTime.Today.AddYears(-1).AddMonths(-1)},
            new Article(){Title="Article A", PostDate=DateTime.Today.AddYears(-1).AddMonths(-2)},
            new Article(){Title="Article A", PostDate=DateTime.Today.AddYears(-1).AddMonths(-3)},
        });

        var set = from article in articles
                  group article by new { article.PostDate.Year, article.PostDate.Month }
                  into byMonthYear
                  group byMonthYear by byMonthYear.Key.Year
                  into byYear
                  select byYear;