how to order a group result with Linq?

Posted by Aaron on Stack Overflow See other posts from Stack Overflow or by Aaron
Published on 2010-05-04T02:15:48Z Indexed on 2010/05/04 2:28 UTC
Read the original article Hit count: 261

Filed under:
|
|

How can I order the results from "group ... by... into..." statement in linq? For instance:

var queryResult = from records in container.tableWhatever
                  where records.Time >= DateTime.Today
                  group records by tableWhatever.tableHeader.UserId into userRecords
                  select new { UserID = userRecords.Key, Records = userRecords };

The query returns records in table "contain.tableWhatever" grouped by "UserId". I want the returned results within each group ordered by time decending. How can I do that?

More specific, assume the above query return only one group like the following:

{UserID = 1, Records= {name1 5/3/2010_7:10pm;
                       name2 5/3/2010_8:10pm;
                       name3 5/3/2010_9:10pm} }

After insert the orderby statement in the above query, the returned results should be like this:

{UserID = 1, Records= {name3 5/3/2010_9:10pm;
                       name2 5/3/2010_8:10pm;
                       name1 5/3/2010_7:10pm} }

Thanks for help!

© Stack Overflow or respective owner

Related posts about LINQ

Related posts about query