Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用Linq排序组结果?_C#_Linq - Fatal编程技术网

C# 如何使用Linq排序组结果?

C# 如何使用Linq排序组结果?,c#,linq,C#,Linq,如何对linq中“group…by…into…”语句的结果进行排序? 例如: var queryResult = from records in container.tableWhatever where records.Time >= DateTime.Today group records by tableWhatever.tableHeader.UserId into userRecords

如何对linq中“group…by…into…”语句的结果进行排序? 例如:

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 };
查询返回表“contain.tablewhere”中按“UserId”分组的记录。我希望每个组内的返回结果按时间递减顺序排列。我该怎么做

更具体地说,假设上述查询只返回一个组,如下所示:

{UserID = 1, Records= {name1 5/3/2010_7:10pm;
                       name2 5/3/2010_8:10pm;
                       name3 5/3/2010_9:10pm} }
在上述查询中插入orderby语句后,返回的结果如下:

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

您能做些什么:

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.OrderByDescending(r=>r.Time) };

只需使用OrderByDescending扩展对匿名类型的记录进行排序

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.OrderByDescending( u => u.Time )
                  };