linq - 如何在 Linq 中订购组结果?

标签 linq sql-order-by group-by

我有以下 linq 查询,效果很好。我不确定如何对分组结果进行排序。

from a in Audits
join u in Users on a.UserId equals u.UserId
group a by a.UserId into g
select new { UserId = g.Key, Score = g.Sum(x => x.Score) }

结果当前按 UserId 升序排序。我追求分数下降。

谢谢:)

最佳答案

只需添加 orderby 子句;-)

from a in Audits
join u in Users on a.UserId equals u.UserId
group a by a.UserId into g
let score = g.Sum(x => x.Score)
orderby score descending
select new { UserId = g.Key, Score = score };

关于linq - 如何在 Linq 中订购组结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/755132/

相关文章:

c# - 使用 LINQ 在 IList 中查找项目

c# - Mysql 到 Linq 查询

sql - 根据列 id(顺序)将所选数据合并到同一行

sql - 为什么添加 RAND() 会导致 MySQL 过载?

sql - 更新受组影响的所有行

c# - 是否可以查询 ExpandoObject 列表?

oracle - 是否可以通过 Oracle 中的绑定(bind)变量引用列名?

mysql - SQL 一对多返回 & 按搜索条件分组

MySQL 按不同的列组合进行分组

c# - LINQ 在运行时选择要查询的表