I have a simple class representing an object. It has 5 properties (a date, 2 decimals, an integer and a string). I have a collection class, derived from CollectionBase, which is a container class for holding multiple objects from my first class.
My question is, I want to remove duplicate objects (e.g. objects that have the same date, same decimals, same integers and same string). Is there a LINQ query I can write to find and remove duplicates? Or find them at the very least?
You can remove duplicates using the
Distinctoperator.There are two overloads – one uses the default equality comparer for your type (which for a custom type will call the
Equals()method on the type). The second allows you to supply your own equality comparer. They both return a new sequence representing your original set without duplicates. Neither overload actually modifies your initial collection – they both return a new sequence that excludes duplicates..If you want to just find the duplicates, you can use
GroupByto do so:To remove duplicates from something like an
IList<>you could do: