繁体   English   中英

Linq按数量分组并按组的最后更新顺序

[英]Linq group by count and order by last update of the group

我需要根据计数编号的降序显示最后的更新行(CreatedDateTime desc)的降序计数。 如何使用LINQ完成?

TableA.GroupBy(c => c.UserID).Select(
                    g => new { UserID = g.Key, Count = g.Count() }
                ).OrderByDescending(c => c.Count)

这是预期排序的示例。

Table A
TableAID  UserID  TableBID CreatedDateTime
1         1       1        ..
2         1       1        ..
3         2       2        ..
4         2       2        ..
......

这样(我希望):-)

TableA.GroupBy(c => c.UserID)
       .Select(g => new { UserID = g.Key, Count = g.Count(), g.Max(h => h.CreatedDateTime) })
       .OrderByDescending(c => c.Count)
       .ThenByDescending(c => c.CreatedDateTime)

请注意,我选择了Max(CreatedDateTime) 我本可以使用Min(CreatedDateTime) ,但结果会有所不同。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM