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

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -