I have a collection where I need to group and find max from that group. So I did
var foo = foobar.GroupBy(x => x.Name)
.Select(x => new Foo { Name = x.Key, Version = x.Max(v => v.Version)))
.ToList();
If there are say more that 2 properties, is it possible to return same object instead of creating new?
Sure, use OrderByDescending
on the group and then First
to get the max-version-object:
var maxVersionObjectByName = foobar
.GroupBy(x => x.Name)
.Select(grp => grp.OrderByDescending(x => x.Version).First())
.ToList();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.