LINQ channel icon LINQ · 26 published articles

Using LINQ Distinct to Select Unique Data

In this post I will teach you how to select unique data within collections using LINQ in .NET. All with live working demo.

Using LINQ Distinct to Select Unique Data
On this page

What you'll learn

In this article, let's learn about how to use Distinct and DistinctBy in LINQ in .NET.

Note: If you have not done so already, I recommend you read the article on Using LINQ Skip to Select Specific Data in Collections.

We can get unique piece of data from beginning of a collection using LINQ. We're going to use Distinct() and DistinctBy() methods. Let's take a look at each of these methods and how they work.

Why it matters

Duplicate data in collections is a common problem. Without Distinct(), you'd need to manually track seen values using a HashSet. Distinct() deduplicates primitive collections, while DistinctBy() provides property-level deduplication for complex objects, returning the first occurrence for each unique key.

Implementation

Get Unique items using Distinct

Sometimes we need to get just a distinct value out of the collection. For instance, we have a bunch of colors in there, we have a few blacks, we have a few violets, we have a few reds,etc., so what we might want to do is say I just want to see the distinct colors, I don't want them all duplicated. So, let's take a look at how we can do that using LINQ and get the unique items.

C#


List<Product> products = GetProducts();

//Method Syntax
List<string> partitionedProducts = products
                                    .Select(product => product.Color)
                                    .Distinct()
                                    .ToList();
                    
//Query Syntax
List<string> partitionedProducts = (from product in products select product)
                                    .Select(product => product.Color)
                                    .Distinct()
                                    .ToList();
        
Live demo: LINQ Distinct Clause Demo — interactive controls are available when the page loads.

Get Unique items using DistinctBy

Instead of just getting colors what if we want the entire product object? We can also extract entire product object conditionally using DistinctBy while the condition evaluates to true. This returns the first product object for each color match and ignores the remaining for same color.

C#


List<Product> products = GetProducts();

//Method Syntax
List<Product> partitionedProducts = products
                                    .DistinctBy(product => product.Color)
                                    .ToList();
                    
//Query Syntax
List<Product> partitionedProducts = (from product in products select product)
                                    .DistinctBy(product => product.Color)
                                    .ToList();
        
Live demo: LINQ DistinctBy Clause Demo — interactive controls are available when the page loads.

Summary

In this article we learn't how to get unique data within collection using Distinct and DistinctBy. We also learnt how to get unique object by passing an expression. All these can be used with any IEnumerable or IQueryable types.