I have a list of objects that has some duplicates by a property. I would like to get all non-duplicate and also 1 of the duplicates based on a condition.
For eg.
Lists:
- Code: 1, Grade: 10
- Code: 1, Grade: 20
- Code: 2, Grade: 1
Expected List:
- Code: 1, Grade: 20
- Code: 2, Grade: 1
The condition would be that of the duplicate elements, grab the one with the highest Grade
. How would I write the lambda or linq expression to do this?
You can use
GroupBy
to do this:Something like
I suggest that you first
GroupBy
theCode
property, and then select theMax
of each element in the group