An alternative way would be
var yourlist = new List<SomeClass>();
// [...]
var uniqueObjs = yourlist.Distinct(); //Gives you a List with unique Objects of the List.
Note that this is only possible, if SomeClass
overrides GetHashCode
and Equals
in some way. This is also true for
var uniqueObjs = new HashSet<SomeType>(yourOriginalList);
Otherwise you could implement you own IEqualityComnparer
-class and pass it to distinct.
Note that with the Distinct()
approach, you can also look for distinct property values of the object in the list:
var uniqueNames = yourlist.Select(obj => obj.Name).Distinct();
and some more...