C# 如何使用Linq排序组结果?
如何对linq中“group…by…into…”语句的结果进行排序? 例如: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
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 )
};