I have a List<MyClass> someList
.
class MyClass { public int Prop1... public int Prop2... public int Prop3... }
I would like to know how to get a new distinct List<MyClass> distinctList
from List<MyClass> someList
, but only comparing it to Prop2
.
C# Linq Distinct() method removes the duplicate elements from a sequence (list) and returns the distinct elements from a single data source. It comes under the Set operators' category in LINQ query operators, and the method works the same way as the DISTINCT directive in Structured Query Language (SQL).
You can emulate the effect of DistinctBy
using GroupBy
and then just using the first entry in each group. Might be a bit slower that the other implementations though.
someList.GroupBy(elem=>elem.Prop2).Select(group=>group.First());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With