如何在Linq订购小组成果?
我有以下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 };
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);
希望这会解决你的问题,比顶级的更容易;)
干杯