Linq实现分组后取最大(小)值

时间:2022-11-25 15:11:40
            var beatles = (new[] {
new { inst = "张三" , age="50" },
new { inst = "张三" , age="20" },
new { inst = "何英" , age="35" },
new { inst = "张三" , age="60" },
new { inst = "何英" , age="18" }
});

var o11 = from d in beatles
group d by d.inst into g
select new
{
Id
= g.Max(m => m.age),
Name
= g.Key
};

 

查询结果是:

inst = "张三" , age="60" 
inst = "何英" , age="35"