Click here to Skip to main content
15,880,469 members
Articles / Programming Languages / C#

DistinctBy in Linq (Find Distinct object by Property)

Rate me:
Please Sign up or sign in to vote.
4.71/5 (13 votes)
28 Jan 2013CPOL3 min read 231K   1.5K   28   12
DistinctBy in Linq (Find Distinct object by Property)
In this article I am going to discuss about how to get distinct object using property  of it from collection. Here I am going to show three different way to achieve it easily.
In this post I am going to discuss about extension method that can do task more than the current Distinct method available in .Net framework.

Distinct method of Linq works as following right now. 
public class Product
{
    public string Name { get; set; }
    public int Code { get; set; }
}
Consider that we have product Class which is having Code and Name as property in it.
Now Requirement is I have to find out the all product with distinct Code values.
Product[] products = { new Product { Name = "apple", Code = 9 }, 
                       new Product { Name = "orange", Code = 4 }, 
                       new Product { Name = "apple", Code = 10 }, 
                       new Product { Name = "lemon", Code = 9 } };
var lstDistProduct = products.Distinct();
foreach (Product p in list1)
{
     Console.WriteLine(p.Code + " : " + p.Name);
}
Output

It returns all the product event though two product have same Code value. So this doesn't meet requirement of getting object with distinct Code value.

Way 1 : Make use of MoreLinq Library
First way to achieve the requirement is make use of MoreLinq Library, which support function called DistinctBy in which you can specify the property on which you want to find Distinct objects.
Below code is shows the use of the function.
var list1 = products.DistinctBy(x=> x.Code);




foreach (Product p in list1)
{
     Console.WriteLine(p.Code + " : " + p.Name);
}
Output

As  you can see in output there is only two object get return which actually I want. i.e. distinct value by Code or product.
If you want to pass more than on property than you can just do like this  var list1 = products.DistinctBy(a => new { a.Name, a.Code });
You can read about the MoreLinq and Download this DLL from here : http://code.google.com/p/morelinq/ one more thing about this library also contains number of other function that you can check.

Way 2: Implement Comparable
Second way to achieve the same functionality is make use of overload Distinct function which support to have comparator as argument.
here is MSDN documentation on this : Enumerable.Distinct<TSource> Method (IEnumerable<TSource>, IEqualityComparer<TSource>)

So for that I implemented IEqualityComparer and created new ProductComparare which you can see in below code.
    class ProductComparare : IEqualityComparer<product>
    {
        private Func<Product, object> _funcDistinct;
        public ProductComparare(Func<Product, object> funcDistinct)
        {
            this._funcDistinct = funcDistinct;
        }




        public bool Equals(Product x, Product y)
        {
            return _funcDistinct(x).Equals(_funcDistinct(y));
        }




        public int GetHashCode(Product obj)
        {
            return this._funcDistinct(obj).GetHashCode();
        }
    }
</product>
So In ProductComparare constructor I am passing function as argument, so when I create any object of it I have to pass my project function as argument.
In Equal method I am comparing object which are returned by my projection function.
Now following is the way how I used this Comparare implementation to satisfy my requirement.
var list2 = products.Distinct(new ProductComparare( a => a.Code ));




            foreach (Product p in list2)
            {
                Console.WriteLine(p.Code + " : " + p.Name);
            }
Output

So this approach also satisfy my requirement easily. I not looked in code of MoreLinq library but I think its also doing like this only. If you want to pass more than on property than you can just do like this  var list1 = products.Distinct(a => new { a.Name, a.Code });.

Way 3: Use GroupBy 
The third and most eaisest way to avoide this I did in above like using MoreLine and Comparare implementation is just make use of GroupBy like as below
List<Product> list = products
                  .GroupBy(a => a.Code )
                  .Select(g => g.First())
                  .ToList();




           foreach (Product p in list)
           {
               Console.WriteLine(p.Code + " : " + p.Name);
           }
In above code I am doing grouping object on basis of property and than in Select function just selecting fist one of the each group will doing work for me.
Output

So this approach also satisfy my requirement easily and output is similar to above two approach. If you want to pass more than on property than you can just do like this   .GroupBy(a => new { a.Name, a.Code }).

So this one is very easy trick to achieve the functionality that I want without using any thing extra in my code.

Conclusion
So Above is the way you can achieve Distinct of collection easily by property of object.

Leave comment if you have any query or if you like it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India

Microsoft C# MVP (12-13)



Hey, I am Pranay Rana, working as a Team Leadin MNC. Web development in Asp.Net with C# and MS sql server are the experience tools that I have had for the past 5.5 years now.

For me def. of programming is : Programming is something that you do once and that get used by multiple for many years

You can visit my blog


StackOverFlow - http://stackoverflow.com/users/314488/pranay
My CV :- http://careers.stackoverflow.com/pranayamr

Awards:



Comments and Discussions

 
QuestionThank you Pin
David Lanham 202222-Oct-22 8:05
David Lanham 202222-Oct-22 8:05 
BugThe code presented shows different input than what the output / rest of the code says Pin
Member 1500795230-Nov-20 7:53
Member 1500795230-Nov-20 7:53 
GeneralRe: The code presented shows different input than what the output / rest of the code says Pin
Member 1500795230-Nov-20 7:55
Member 1500795230-Nov-20 7:55 
QuestionIs the output correct? Pin
George Swan28-Jan-13 8:58
mveGeorge Swan28-Jan-13 8:58 
AnswerRe: Is the output correct? Pin
Pranay Rana28-Jan-13 9:21
professionalPranay Rana28-Jan-13 9:21 
QuestionApple 10 Pin
rrrado28-Jan-13 0:36
rrrado28-Jan-13 0:36 
GeneralMy vote of 5 Pin
SRIRAM 227-Jan-13 21:40
SRIRAM 227-Jan-13 21:40 
GeneralRe: My vote of 5 Pin
Pranay Rana27-Jan-13 23:55
professionalPranay Rana27-Jan-13 23:55 
Question[My vote of 2] source ?? Pin
Sperneder Patrick27-Jan-13 20:38
professionalSperneder Patrick27-Jan-13 20:38 
AnswerRe: [My vote of 2] source ?? Pin
Pranay Rana27-Jan-13 23:04
professionalPranay Rana27-Jan-13 23:04 
GeneralRe: [My vote of 2] source ?? Pin
Sperneder Patrick27-Jan-13 23:47
professionalSperneder Patrick27-Jan-13 23:47 
GeneralRe: [My vote of 2] source ?? Pin
Pranay Rana27-Jan-13 23:54
professionalPranay Rana27-Jan-13 23:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.