40

我有以下 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 升序排列。我在分数下降之后。

谢谢 :)

4

2 回答 2

65

只需添加 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 };
于 2009-04-16T08:02:18.067 回答
13
var results =
 (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) })
.OrderByDescending(p=>p.Score);

希望这能解决你的问题,比上面的更容易;)

干杯

于 2009-04-16T08:02:31.403 回答