LINQ DistinctBy

Summary: in this tutorial, you will learn how to use the LINQ DistinctBy() method to return distinct elements from a sequence according to a specified key selector function.

Introduction to the LINQ DistinctBy method

The DistinctBy() method allows you to get the distinct elements from a sequence based on a specified key selector function.

Here’s the syntax of the DistinctBy() method:

IEnumerable<TSource> DistinctBy<TSource,TKey> (
    this IEnumerable<TSource> source, 
    Func<TSource,TKey> keySelector
);Code language: C# (cs)

In this syntax:

  • TSource is the type of elements of source sequence.
  • TKey is the type of key used to distinguish elements.
  • source is the input sequence with the IEnumerable<T> type.
  • keySelector is a function that extracts the key for each element.

The DistinctBy method returns an IEnumerable<T> that contains unique elements from the source sequence.

LINQ DistinctBy() method example

Suppose you have a Product class that has Id, Name, and Price:

public class Product
{
    public int? Id
    {
        get; set;
    }
    public string? Name
    {
        get; set;
    }
    public decimal? Price
    {
        get; set;
    }

    public override string ToString()
    {
        return $"{Id} {Name} {Price:C}";
    }

}Code language: C# (cs)

The following example uses the DistinctBy() method to find unique products by Id:

using static System.Console;

List<Product> products = new() 
{
    new Product { Id =1, Name = "A", Price=9.99M },
    new Product { Id =2, Name = "B", Price=15.99M },
    new Product { Id =3, Name = "C", Price=5.99M },
    new Product { Id =1, Name = "A", Price=9.99M },
    new Product { Id =2, Name = "B", Price=15.99M },
};

var results = products.DistinctBy(p => p.Id);

foreach (var product in results)
{
    WriteLine(product);
}Code language: C# (cs)

Output:

1 A $9.99
2 B $15.99
3 C $5.99Code language: C# (cs)

In this example, we use the following key selector function for the DistinctBy() method:

p => p.IdCode language: C# (cs)

The key selector function accepts an argument which is an instance of the Product class. It returns the Id property used for comparing the product objects.

Note that without the DistinctBy() method, you have to define a custom equality comparer (IEqualityComparer) to compare two products by Id.

To get distinct products by Name, you use a different key selector function that returns the Name property of a product instead:

using static System.Console;

List<Product> products = new() 
{
    new Product { Id =1, Name = "A", Price=9.99M },
    new Product { Id =2, Name = "B", Price=15.99M },
    new Product { Id =3, Name = "C", Price=5.99M },
    new Product { Id =1, Name = "A", Price=9.99M },
    new Product { Id =2, Name = "B", Price=15.99M },
};

var results = products.DistinctBy(p => p.Name);

foreach (var product in results)
{
    WriteLine(product);
}Code language: C# (cs)

Output:

1 A $9.99
2 B $15.99
3 C $5.99Code language: C# (cs)

Summary

  • Use the DistinctBy() method to return distinct elements from a sequence according to a specified key selector function.
Was this tutorial helpful ?