[英]How to select MAX ID using LINQ?
From the sql data below, I'd like to take a distinct EmpId that is the max ID. 从下面的sql数据中,我想采用一个不同的EmpId作为最大ID。
ID EmpId DeptId
1 1002 XY
5 1100 ABC
6 1109 EF
7 1100 MN
9 1100 DE
10 1250 CE
11 1250 DJ
12 1100 DE
Results would look like the following: 结果如下所示:
ID EmpId DeptId
1 1002 XY
6 1109 EF
11 1250 DJ
12 1100 DE
How should this LINQ be structured? LINQ应该如何组织?
var result = list.GroupBy(x=>x.EmpId).Select(g=>g.OrderByDescending(y=>y.Id).First());
from e in context.Employees
group e by e.EmpId into g
select new {EmpId = g.Key,
ID = g.OrderByDescending(gg=>gg.ID).FirstOrDefault().ID,
DeptId = g.OrderByDescending(gg=>gg.ID).FirstOrDefault().DeptId
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.