Friday, 2 October 2015

DistinctBy in LINQ

The operator DistinctBy is lacking in LINQ, but we can achieve it with the GroupBy operator.

using System;
using System.Collections.Generic;
using System.Linq;

namespace GroupingLinq

        public static class EnumerableExtensions

            /// <summary>
            /// Returns a ienumerable which is distinct by a given property key selector. If a custom equality 
            /// comparer is to be used, pass this in as the comparer. By setting the comparer default to null,
            /// the default comparer is used. 
            /// </summary>
            /// <typeparam name="T">The item type in the ienumerable</typeparam>
            /// <typeparam name="TKey">The type of the key selector (property to disinct elements by)</typeparam>
            /// <param name="coll">The source ienumerable</param>
            /// <param name="keySelector">The key selector, use a member expression in a lambda expression</param>
            /// <param name="comparer">Custom comparer to use, pass in null here to specify that default comparer is used,
            /// however, this is default set to null and not required parameter</param>
            /// <returns></returns>
            public static IEnumerable<T> DistinctBy<T, TKey>(this IEnumerable<T> coll, Func<T, TKey> keySelector,
                IEqualityComparer<TKey> comparer = null)
                if (coll == null)
                    throw new ArgumentNullException("coll");
                if (keySelector == null)
                    throw new ArgumentNullException("keySelector");

                var result = coll.GroupBy(keySelector, comparer).Select(g => g.First()).ToList();
                return new List<T>(result).AsEnumerable();



The following entertaining example which is a console application shows how we can use this operator:

class Program

        static void Main(string[] args)

            var cars = new []
                new Car {Model = "Audi", Make = "A4", Color = "Black"},
                new Car {Model = "Audi", Make = "A8", Color = "Red"},
                new Car {Model = "Audi", Make = "TT", Color = "Black"},
                new Car {Model = "Volvo", Make = "XC90", Color = "Black"},
                new Car {Model = "Volvo", Make = "S90", Color = "Black"},
                new Car {Model = "Ferrari", Make = "F500", Color = "Yellow"},
                new Car {Model = "Ferrari", Make = "F500", Color = "Red"},
                new Car {Model = "Lada", Make = "Limousine", Color = "Rusty"}

            var groupedCars = cars.DistinctBy(c => new {c.Model, c.Color});

            foreach (var gc in groupedCars)

            Console.WriteLine("Press any key to continue ...");

        // Define other methods and classes here


The sample output is then:

Model: Audi, Make: A4, Color: Black
Model: Audi, Make: A8, Color: Red
Model: Volvo, Make: XC90, Color: Black
Model: Ferrari, Make: F500, Color: Yellow
Model: Ferrari, Make: F500, Color: Red
Model: Lada, Make: Limousine, Color: Rusty
Press any key to continue ...

In the example we used DistinctBy some Cars based on their properties Model and Color. We want in other words unique combinations of Model AND Color. I.e. we can have multiple Cars of the same Model but WITH different Color OR we can have multiple cars with same Color but then the Model but be different. Uniqueness Criterion: (Model, Color) We did not get the item "Audi TT Black" because we already got a black Audi. We did not get the "Volvo S90 Black" because we already got a black Volvo. We got both the Ferrari F500 because they got different colors. And sadly, we are stuck with the "Lada Limousine Rusty" since it was the only combination of Model and Color.

No comments:

Post a Comment