How do I separate items use linq?

user3149944

I have a dataset for an articles web site. Each article has datetime, but I can't separate them according to datetime with Linq.

For Example, this web site.

As you can see dataset generates month month and year year.

Using linq how can I separate this dataset?

I thought maybe I can if I use like this List<List<Article>> but this defeat.

Say I have a class:

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

or a Datarow with a PostDate column

And a list of these Articles:

 List<Article>

I'd like to group these articles into first Year and then Month, based on the PostDate using Linq.

So that I can output them as follows:

  • 2013 | Article.PostDate.Year
    • December | Article.PostDate.Month
      • Article Title A | Article.Title
      • Article Title B
    • November
    • October
    • ..
  • 2012
    • December
    • November
    • October
    • ...
Damir Arh

I understand, you're trying to group the articles by year and then by month.

Let's say you have the following Article class:

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

    public Article(DateTime postDate, string title)
    {
        PostDate = postDate;
        Title = title;
    }
}

And the following sample data:

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

You could do the grouping like this:

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

And output it like this:

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

Here's the output:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I use “separate" keyword

How do i use separate {tidyr} to separate when number occurs?

How do I use tidyr::separate to separate a factor including a number?

How do I use First() in LINQ but random?

How do I separate multiple items within a div class into separate lists?

How do I use a random number I generated in a separate sum

How can I create separate list items from local storage items and show them in to do list?

How do I use a class method in a separate program?

How do I use my home directory on a separate partition?

How do I use map in React Native to create separate buttons?

How do I use cut to separate by multiple whitespace?

How do I use ServiceWorker without a separate JS file?

How do I use a for loop to store function output in separate variables?

How do I use a Firefox 4 profile on a separate drive/partition

How do i use my items in my Menu?

How do I use Any() instead of RemoveAll() to exclude list items?

How do I use ParseJson in VBA to get selected items in response?

How do I use tkinter 'Treeview' to list items in a table of a database?

sqlite - how can I know Oldest items do not use

How do i use 2 separate functions "fgets" for writing into 2 separate variables?

How do I separate subplots?

How do I use LINQ to .Select from a List inside a Map?

How do I use func in Linq to Entity select?

How can I use LINQ to select a property and also other items of a list at the same level?

How can I use a LINQ statement to filter out items that matches one of the words from another List?

How do I count the number of child collection's items using LINQ Method Syntax?

How do I create a linq statement to take a list of items and break them into groups of 50 each

How do I remove items from generic list, based on multiple conditions and using linq

Why should I use a separate test host for running XCTests and how should I do that?