如何使用linq分隔项目?

时间:2013-12-31 18:06:15

标签: c# linq

我有一个文章网站的数据集。每篇文章都有日期时间,但我不能根据日期时间与Linq分开。

例如,这是web site

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

使用linq如何分离此数据集?

我想也许我可以这样使用List<List<Article>>而不是这个失败。

说我有课:

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

或具有PostDate

的Datarow

以及这些文章的清单:

 List<Article>

我想根据使用Linq的Year将这些文章分组到第一个Month然后PostDate

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

  • 2013 | Article.PostDate.Year
    • 12月 | Article.PostDate.Month
      • 文章标题A | Article.Title
      • 文章标题B
    • 十一月
    • 十月
    • ..
  • 2012
    • 十一月
    • 十月
    • ...

3 个答案:

答案 0 :(得分:1)

如果我理解你的问题,那么你希望能够逐年分组,然后按月分组。

这是一种方式:

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);
            }
        }
    }
}

将输出:

Year: 2013 Month: 12 PostDate: 12/31/2013 11:40:48 AM
Year: 2013 Month: 12 PostDate: 12/31/2013 11:41:48 AM
Year: 2014 Month: 1 PostDate: 1/31/2014 11:40:48 AM
Year: 2014 Month: 1 PostDate: 1/31/2014 11:41:48 AM
Year: 2014 Month: 2 PostDate: 2/28/2014 11:40:48 AM
Year: 2014 Month: 2 PostDate: 2/28/2014 11:41:48 AM
Year: 2014 Month: 4 PostDate: 4/30/2014 11:40:48 AM
Year: 2014 Month: 4 PostDate: 4/30/2014 11:41:48 AM

答案 1 :(得分:1)

我理解,您正在尝试按年份分组文章,然后按月分组。

我们假设您有以下文章类:

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

答案 2 :(得分:1)

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

        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;