c# - How to distinct a list using LINQ? -
i have class event
have 2 properties : "id", , "expirationtime". have list have many events, of them same id. want create an efficient linq query distinct events id, , each id keep event smallest expirationtime.
thanks!
the grouping easy enough, doing efficient "minby" standard linq objects messy:
var lowestbyid = items.groupby(x => x.id) .select(group => group.aggregate((best, next) => best.expirationtime < next.expirationtime ? best : next));
it's cleaner minby
operator, such 1 provided morelinq.
var lowestbyid = items.groupby(x => x.id) .select(group => group.minby(x => x.expirationtime));
Comments
Post a Comment