Improving Upon LINQ’s Distinct

August 1, 2009

Suppose you have a collection of objects, and you want only the distinct values. You could easily use Linq for this, like that: var items = new {"BMW","Fiat","Ferrari","Fiat"}; var distinctItems = items.Distinct(); This uses the default comparer for the objects in order to see if they’re equal. There’s also another overload, which lets you specify your own comparer. This is its signature: public static IEnumerable<TSource> Distinct<TSource>(this IEnumerable<TSource> source, IEqualityComparer<TSource> comparer); It accepts an IEqualityComparer to compare between items. But the usage of IEqualityComparer is...
tags: ,