如何使用Linq获取每个组中的第一条记录
问题描述:
考虑以下记录:
Id F1 F2 F3
-------------------------------------------------
1 Nima 1990 10
2 Nima 1990 11
3 Nima 2000 12
4 John 2001 1
5 John 2002 2
6 Sara 2010 4
我想根据 F1
字段分组并按 Id
排序并从与这些记录相似的组的第一条记录中获取所有字段:
I want to group by based on the F1
field and sort by Id
and get all fields from the first record of group similar to these records:
Id F1 F2 F3
-------------------------------------------------
1 Nima 1990 10
4 John 2001 1
6 Sara 2010 4
如何使用 linq 执行此操作?
How can I do this using linq?
答
var res = from element in list
group element by element.F1
into groups
select groups.OrderBy(p => p.F2).First();