Tuesday, April 2, 2013

C# LINQ Group by key and get max value of group

This is a handy C# LINQ snippet to group by a key in a list of objects and return a new list with only max values based on another property:

SomeList.GroupBy(x => x.MyKey).Select(g => g.OrderByDescending(x => x.DateCreated).First());

This was a stackoverflow answer - thanks driis!