有没有更好的方法来获得完整的一行计算组使用LINQ

xriantvc  于 9个月前  发布在  其他
关注(0)|答案(2)|浏览(89)

这个问题是对hx 1 e0f1x的进一步的问题。除了最大值和组键,我还需要行的每一列(包括最重要的一列:ID)。
例如,给定学生表
| ID|名称|类|评分|
| --|--|--|--|
| 1 |N0| C0| 0 |
| 2 |N1| C0| 1 |
| 3 |N2| C1| 1 |
| 4 |N3| C1| 2 |
| 5 |N4| C1| 3 |
| 6 |N5| C2| 4 |
| 7 |N6| C2| 5 |
为了让整排最高分的学生按班级分组,我想出了一个方法,但不满意它的简单性:

from o in Students
join o1 in
  (from oo in Students
   group oo by oo.Class
   into g
   select new { Class = g.Key, MaxScore = g.Max(x => x.Score)})
on o.Class + o.Score equals o1.Class + o1.MaxScore 
select o

字符串
我的问题是:有没有更简单的方法来实现以上使用LINQ或原始MSSQL?

vmdwslir

vmdwslir1#

你可以做一些事情,比如从分组中选择,这应该使用窗口函数。

from o in Students
group o by o.Class into g
from s in g
where s.Score == g.Max(x => x.Score)
select s;

字符串
另一种选择,它不会给给予捆绑的结果

from o in Students
group o by o.Class into g
select g.OrderByDescending(x => x.Score).First();

xdnvmnnf

xdnvmnnf2#

你也可以像这样拆分它:
1.将学生按班级分组,并找出每个班级的最高分。
1.加入学生在各自班级中获得最高分数的原始列表。

相关问题