Geeks With Blogs

News

Microsoft MVP


DZone MVB


Moderator at CodeASP.NET


Quiz Master







free counters
Free counters
Added on January 19,2012


Follow Me @vmsdurano

A bit About Me



Disclaimer
The opinions expressed herein are my own personal opinions and does not represent the opinions of my employers. Nor does it represent the opinion of my dog, because I don’t have one.


Vinz' Blog (ProudMonkey) "Code, Beer and Music ~ my way of being a programmer"

This article talks about the different scenarios regarding filtering distinct values from the List<T>. One practical example is if you have a list of products and wanted to get the distinct values from the list. To make it more clear let’s take an example. Consider that we have this model below that houses the following properties:

public class Product
{
        public int ProductID { get; set; }
        public string Make { get; set; }
        public string Model { get; set; }
}

Now let’s create a method that would create a list of Products. For example:

private List<Product> GetProducts() {
    List<Product> products = new List<Product>();
    products.Add(new Product { ProductID = 1, Make = "Samsung", Model = "Galaxy S3" });
    products.Add(new Product { ProductID = 2, Make = "Samsung", Model = "Galaxy S4" });
    products.Add(new Product { ProductID = 3, Make = "Samsung", Model = "Galaxy S5" });
    products.Add(new Product { ProductID = 4, Make = "Apple", Model = "iPhone 5" });
    products.Add(new Product { ProductID = 5, Make = "Apple", Model = "iPhone 6" });
    products.Add(new Product { ProductID = 6, Make = "Apple", Model = "iPhone 6" });
    products.Add(new Product { ProductID = 7, Make = "HTC", Model = "Sensation" });
    products.Add(new Product { ProductID = 8, Make = "HTC", Model = "Desire" });
    products.Add(new Product { ProductID = 9, Make = "HTC", Model = "Desire" });
    products.Add(new Product { ProductID = 10, Make = "Nokia", Model = "Lumia 735" });
    products.Add(new Product { ProductID = 11, Make = "Nokia", Model = "Lumia 930" });
    products.Add(new Product { ProductID = 12, Make = "Nokia", Model = "Lumia 930" });
    products.Add(new Product { ProductID = 13, Make = "Sony", Model = "Xperia Z3" });

    return products;
}

The method above returns a list of Products by adding a dummy data to the List<Product> just for the simplicity of this demo. In real scenario you may want to query your database and load the result to your model. Now let’s bind the Products data in GridView.

protected void Page_Load(object sender, EventArgs e) {
    if (!IsPostBack) {
        GridView1.DataSource = GetProducts();
        GridView1.DataBind();
    }
}

Running the code will give you the following output below:

Capture

If you noticed there are few items above that contain the same values or commonly called “duplicate” values. Now let’s try to get the distinct row values from the list using the LINQ Distinct function. The code now would look like this:

if (!IsPostBack) {
        GridView1.DataSource = GetProducts().Distinct();
        GridView1.DataBind();
}

Unfortunately, running the code will still give you the same output. This means that the Distinct LINQ function doesn’t work at all. I was surprised and my first reaction was like…

shocked

What??? Really???

Yes, it doesn’t work as expected! This is because the Distinct method uses the Default equality comparer to compare values under the hood. And since we are dealing with reference type object then the Distinct will threat the values as unique even if the property values are the same.

So how are we going to deal with this?

There are few possible ways to accomplish this and these are:

Option 1: Using a combination of LINQ GroupBy and Select operators

if (!IsPostBack) {
        GridView1.DataSource = GetProducts()
                               .GroupBy(o => new { o.Make, o.Model })
                               .Select(o => o.FirstOrDefault());
        GridView1.DataBind();
}

Running the code will give you the following output below:

Capture2

Option 2: Using a combination of LINQ Select and Distinct operators

if (!IsPostBack) {
        GridView1.DataSource = GetProducts()
                               .Select(o => new { o.Make, o.Model })
                               .Distinct();
        GridView1.DataBind();
}

The approach above creates a collection of an anonymous type. Doing a Distinct on the anonymous types will automatically override the Equals and GetHashCode to compare each property thus giving us unique values. Running the code will give you the following output below:

Capture3

Option 3: Using the IEqualityCompare<T> interface

class ProductComparer : IEqualityComparer<Product>
{
    public bool Equals(Product x, Product y) {
        if (Object.ReferenceEquals(x, y)) return true;

        if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
            return false;

        return x.Make == y.Make && x.Model == y.Model;
    }
    public int GetHashCode(Product product) {
        if (Object.ReferenceEquals(product, null)) return 0;
        int hashProductName = product.Make == null ? 0 : product.Make.GetHashCode();
        int hashProductCode = product.Model.GetHashCode();
        return hashProductName ^ hashProductCode;
    }
 }

The Distinct operator has an overload method that lets you pass an instance of IEqualityComparer. So for this approach we created a class “ProductComparer” that implements the IEqualityCompaper. Here’s the code to use it:

if (!IsPostBack) {
        GridView1.DataSource = GetProducts()
                               .Distinct(new ProductComparer());
        GridView1.DataBind();
}

This approach is my preferred option because it allows me to implement my own GetHashCode and Equals methods for comparing custom types. Also getting into a habit of making interfaces makes your code more reusable and readable. Here’s the output below:

Capture2

As you observe the duplicate values are now gone. Now here’s another scenario. What if we want to get the distinct values for a certain field in the list? For example get the distinct “Make” values such as Samsung, Apple, HTC, Nokia and Sony and then populate the result to a DropDownList control for filtering purposes. I was hoping that the Distinct function has an overload that can compare values based on a property or field like GetProducts().Distinct(o => o.PropertyToCompare) but then again it doesn’t seem to have that overload. So I came up with the following workarounds:

Option 1: Using GroupBy and Select operators

if (!IsPostBack) {
        DropDownList1.DataSource = GetProducts()
                                   .GroupBy(o => o.Make)
                                   .Select(o => o.FirstOrDefault());
        DropDownList1.DataTextField = "Make";
        DropDownList1.DataValueField = "Make";
        DropDownList1.DataBind();
}

Option 2: Using Select and Distinct operators

if (!IsPostBack) {
        DropDownList1.DataSource = GetProducts()
                                   .Select(o => new { Make = o.Make } )
                                   .Distinct();
        DropDownList1.DataTextField = "Make";
        DropDownList1.DataValueField = "Make";
        DropDownList1.DataBind();
}

Running the code for both options will give this output below:

Capture4

That’s it! I hope you will find this post useful. Smile

Posted on Thursday, May 7, 2015 7:43 PM | Back to top


Comments on this post: Different ways to get Distinct Values from a List<T> using LINQ

comments powered by Disqus

Copyright © Vincent Maverick Durano | Powered by: GeeksWithBlogs.net