Selecting distinct values in a Linq query while working on non trivial data need more than a simple call to the extension method Distinct().
Say I have the following xml data, and I want to select only distinct data elements based on their date attribute.
As you can see there are some duplicates regarding the date attribute, the following Linq query will return all the data elements as an IEnumerable of HistoryDataElement.
If we want distinct results based on the date attribute for example, we have to create a comparison class that implements the interface IEqualityComparer<T>.
Here is a very simple implementation:
With this class in place, we can just pass a new instance of this class to the Distinct method to get the desired results :
Notice that this way, you are eliminating all the duplicates that have the same property date, so only the first element of the matching elements is retrieved even If the other properties differ, this is cited by code in the Equals method of the IEqualityComparer<T>.