locked
Ling orderby and groupby RRS feed

  • Question

  • I have the following code. And I want the group by groups (i.e. the years and months) to be sorted ascendingly. How can I do this? Thanks.

    Code Snippet

                    var AList = from an in db.Announcements
                                group an by an.EnteredOn.Year into YearGroups                         
                                select
                                    new
                                    {
                                        Year = YearGroups.Key,
                                        MonthGroups =
                                            from an2 in YearGroups
                                            group an2 by an2.EnteredOn.Month into MonthGroups
                                            select
                                                new
                                                {
                                                    Month = MonthGroups.Key,
                                                    Announcements = MonthGroups
                                                }
                                    };

    Wednesday, May 14, 2008 11:09 AM

Answers

  • Ok I have the months, years and announcements ordered. Yay!


    Code Snippet

                    var AList = from an in db.Announcements
                                group an by an.EnteredOn.Year into YearGroups  
                                orderby YearGroups.Key descending
                                select
                                    new
                                    {
                                        Year = YearGroups.Key,
                                        MonthGroups =
                                            from an2 in YearGroups
                                            group an2 by an2.EnteredOn.Month into MonthGroups  
                                            orderby MonthGroups.Key descending                                       
                                            select
                                                new
                                                {
                                                    Month = MonthGroups.Key,
                                                    Announcements =
                                                        (from an3 in MonthGroups
                                                                    orderby an3.EnteredOn descending
                                                                    select an3)
                                                    
                                                }
                                    };


    Wednesday, May 14, 2008 2:32 PM