c# - Find the minimum value with the same id in a list -


i have list of objects structure similar this: id, operationid, prop1, prop2, ...

i must remove list object have same id have max value operationid.. example have instance of list:

new list<myobject> {     new myobject() { id = 1, operationid = 1, prop1, prop2, ... },     new myobject() { id = 1, operationid = 2, prop1, prop2, ... },     new myobject() { id = 2, operationid = 1, prop1, prop2, ... },     new myobject() { id = 2, operationid = 2, prop1, prop2, ... },     new myobject() { id = 3, operationid = 2, prop1, prop2, ... } } 

the result must be:

new list<myobject> {     new myobject() { id = 1, operationid = 1, prop1, prop2, ... },     new myobject() { id = 2, operationid = 1, prop1, prop2, ... },     new myobject() { id = 3, operationid = 2, prop1, prop2, ... } } 

an important thing: don't have find id , operationid.. result must whole object!

i have tried comparer, lambda expression, distinct, grouping , select, haven't find solution...

how can solve?

thanx, simone

you can use query:

myobjects = myobjects     .groupby(obj => obj.id)     .select(grp => grp.orderby(obj => obj.operationid).first())     .tolist(); 

that removes duplicates according id , lowest operationid remains.


Comments