LINQ DistinctBy

Using DistinctBy to get a unique collection of items

Home DailyDrop

Daily Knowledge Drop

The Enumerable DistinctBy method, introduced in .NET 6, can be used to return distinct elements from a sequence according to the specified key selector.

Prior to .NET 6, performing this operation was a bit complex, but the introduction of the DistinctBy method simplifies the process.


Setup

In this example, a collection of 3 Songs will be used:

List<Song> songs = new List<Song>
{
    new Song
    {
        Id = 1,
        Name = "Everlong",
        Album = "The Colour and the Shape"
    },
    new Song
    {
        Id = 2,
        Name = "Monkey Wrench",
        Album = "The Colour and the Shape"
    }
    ,
    new Song
    {
        Id = 3,
        Name = "Learn to Fly",
        Album = "There Is Nothing Left to Lose"
    }
};

Let's look at how we can get a distinct list of albums from the collection of songs.


Pre .NET 6

The process for doing this (using LINQ) prior to .NET 6 was as follows:

IEnumerable<string> albums = songs
    .GroupBy(s => s.Album)
    .Select(g => g.First())
    .Select(a => a.Album);

The steps are as follows:

  • Use GroupBy to get a collection of Songs, with the Album value as the key
  • Use Select and First to get the first record in each group
  • Use Select to get the Album of the first record

As the Songs in each group will all contain the same Album (as we are grouping by Album), selecting any record will yield the same Album.

This works, but is a fairly convoluted process.


.NET 6 and beyond

The DistinctBy method greatly simplifies this:

IEnumerable<string> albums = songs
    .DistinctBy(s => s.Album)
    .Select(a => a.Album);

Here, the steps are as follows:

  • Use DistinctBy to get a collection of Songs, each one having a unique Album value. In this example, after this is executed, the record with id = 2 is dropped from the collection as it has the same Album value as thew previous record
  • Use Select to get the Album of each of the remaining records

Easier to read and less convoluted!


Notes

The addition of the new DistinctBy makes a big difference to the complexity, as well as the readability of the code - instead of trying to decider some complex LINQ (as was the case prior to .NET 6), the code now better describes what it is actually doing.


References

Do you know about the DistinctBy method?

Daily Drop 229: 06-01-2023

At the start of 2022 I set myself the goal of learning one new coding related piece of knowledge a day.
It could be anything - some.NET / C# functionality I wasn't aware of, a design practice, a cool new coding technique, or just something I find interesting. It could be something I knew at one point but had forgotten, or something completely new, which I may or may never actually use.

The Daily Drop is a record of these pieces of knowledge - writing about and summarizing them helps re-enforce the information for myself, as well as potentially helps others learn something new as well.
c# .net linq distinctby