Grouping LINQ/SQL results RRS feed

  • Question

  • Hi,

    I have a LINQ to SQL class "Announcement". Within this class is a field "EnteredOn" which is of type DateTime. Basically what I want to do is get a list of lists out; each sub list will contain announcements from each unique Month Year. So I can basically display each group of announcements on an ajax accordion for people to browse by Month.

    Is there a clever jazzy way to do this in LINQ. Otherwise I'll  just do a seperate LINQ query for each possible month. Unless there is a better way (I hope...)?

    Friday, May 2, 2008 3:41 PM


  • The result of a LINQ group-by is a sequence of sequences.


    var query =

         from a in announcements

         group a by a.EnteredOn;


    foreach(var item in query)


         Console.WriteLine("EnteredOn: {0}", item.Key);

         foreach(Announcement a in item)


             Console.WriteLine("Announcement: {0}", a.Something);



    Friday, May 2, 2008 3:53 PM