Reputation: 1013
public class Foo
{
public string Name {get; set;}
public int Year {get; set;}
}
Say i have a List of Foo items. It is possible that some Foo items have the same name. In that case i want to filter out all but the most recent Foo item with that name. ( A Foo item can not have the same name AND the same Year )
Is there way to do this with a single LINQ statement ?
Upvotes: 0
Views: 64
Reputation: 11238
You can use GroupBy
:
var result = items.GroupBy(foo => foo.Name)
.Select(g => g.OrderByDescending(foo => foo.Year).First())
.ToList();
Upvotes: 5