我希望获取枚举中所有属性值最大的元素:
IEnumerable<Person> allOldest = people.GroupBy(p => p.Age)
.OrderByDescending(i=>i.Key).First().Select(_=>_);
.NET 6引入了MaxBy
,它返回最大项:
Person uniqueOldest = people.MaxBy(person => person.Age);
MaxBy
对我没用吗?还有比第一个例子更优雅的解决方案吗?
Max
来获取最大值,然后使用Where
获取所有具有该值的元素。 - Jon Skeet