Finding unique values using the LINQ Distinct operator
November 18, 2016 Leave a comment
Extracting unique values from a sequence of objects in LINQ is very easy using the Distinct operator. It comes in two versions: one with a default equality comparer and another which lets you provide a custom comparer.
We’ll use the following collection for the first demo:
string[] bands = { "ACDC", "Queen", "Aerosmith", "Iron Maiden", "Megadeth", "Metallica", "Cream", "Oasis", "Abba", "Blur", "Chic", "Eurythmics", "Genesis", "INXS", "Midnight Oil", "Kent", "Madness", "Manic Street Preachers" , "Noir Desir", "The Offspring", "Pink Floyd", "Rammstein", "Red Hot Chili Peppers", "Tears for Fears" , "Deep Purple", "KISS"};
These are all unique values so let’s create some duplicates:
IEnumerable<string> bandsDuplicated = bands.Concat(bands);
The first prototype of Distinct will compare the string values using the standard default string comparer as all elements in the array are strings. The following bit of code will find the unique values and print them on the console:
IEnumerable<string> uniqueBands = bandsDuplicated.Distinct(); foreach (String unique in uniqueBands) { Console.WriteLine(unique); }
In case you have a sequence of custom objects then it’s a good idea to let the Distinct operator know how to define if two objects are equal. Consider the following object:
public class Singer { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } }
…and the following collection:
IEnumerable<Singer> singers = new List<Singer>() { new Singer(){Id = 1, FirstName = "Freddie", LastName = "Mercury"} , new Singer(){Id = 2, FirstName = "Elvis", LastName = "Presley"} , new Singer(){Id = 3, FirstName = "Chuck", LastName = "Berry"} , new Singer(){Id = 4, FirstName = "Ray", LastName = "Charles"} , new Singer(){Id = 5, FirstName = "David", LastName = "Bowie"} };
The following comparer will compare the Singer objects by their IDs:
public class DefaultSingerComparer : IEqualityComparer<Singer> { public bool Equals(Singer x, Singer y) { return x.Id == y.Id; } public int GetHashCode(Singer obj) { return obj.Id.GetHashCode(); } }
We create some duplicates:
IEnumerable<Singer> singersDuplicates = singers.Concat(singers);
…and then select the unique values:
IEnumerable<Singer> uniqueSingers = singersDuplicates.Distinct(new DefaultSingerComparer()); foreach (Singer singer in uniqueSingers) { Console.WriteLine(singer.Id); }
You can view all LINQ-related posts on this blog here.