Linq MaxBy 包含所有元素?

New*_*Tom 5 c# linq max .net-6.0

我想获取可枚举中属性为最大值的所有元素:

IEnumerable<Person> allOldest = people.GroupBy(p => p.Age)
    .OrderByDescending(i=>i.Key).First().Select(_=>_);
Run Code Online (Sandbox Code Playgroud)

.NET 6 引入了MaxBy返回最大项的功能:

Person uniqueOldest = people.MaxBy(person => person.Age);
Run Code Online (Sandbox Code Playgroud)

MaxBy不能帮助我吗?还有比第一个示例更优雅的解决方案吗?

Ran*_*ner 5

使用Max是一种简单明了的方法:

var maxAge = items.Max(y => y.Age);
var maxItems = items.Where(x => x.Age == maxAge);
Run Code Online (Sandbox Code Playgroud)