I have a List<T> where T is a custom object. None of my object are equal but some might have an equal property. Is there any fast way to remove the duplicates by comparing the property? It doesn’t matter which of the duplicates stays in the list.
I have a List<T> where T is a custom object. None of my object
Share
You can use
List<T>.RemoveAllto do this efficiently.For example, if you wanted to remove all elements where the
Fooproperty had a value of 42, you could do:If you’re trying to make a list of distinct items by a property, ie: keep only distinct Foo items, I would recommend doing something like:
This will track which elements are “distinct” and remove all others.