Language Integrated Query (LINQ) is a powerful tool within the .NET framework that allows developers to more easily work with lists and collections of objects. In my opinion, LINQ is probably the best time-saving feature within a .NET developers toolkit and understanding everything it can do will help you code much quicker.

Even if you think you are a LINQ ninja, did you know that Microsoft are continually adding to what it can do and unless you religiously keep up-to-date with .NET releases, it is likely there are a few new methods you are unaware of.

To make sure you are up-to-date, I will go over the last 7 new updates to LINQ, including all the new upcoming.NET 9 updates🔥🔥🔥

CountBy

The first method in this list is a new .NET 9 feature and its called CountBy(). CountBy() will take a collection and return all the unique elements within that list as well as a count of how many occurrences that element appears within the collection.

This method will allow you to efficiently tally the occurrences of each unique element in a collection. Without this extension, the code you could have written could have looked like this:

AggregateBy()

As well as CountBy(), .NET 9 also ships with AggregateBy(). Personally, I don't think the intention of what AggregateBy() does is immediately obvious just from the name. The way I find it easier to remember what AggregateBy() does is to mentally group it with CountBy().

CountBy() is the simple no-frills way to count the number of unique elements within a collection. As a developer, you have no control over the format of data that CountBy() returns, however, what happens when you need to change the format? For example, imagine you are working with items in a shopping basket and instead of a item count, you want the item total price instead.

Instead of writing a custom implementation, you use AggregateBy() instead. AggregateBy() will allow you to specify a key selector and an aggregator function. Within the aggregator function you can control the format of the data that is returned. The calculation is the same as 'CountBy()', however, the difference is the control over what you want returned!

Index()

This is another .NET 9 newcomer and when you think about it, its pretty surprising this method didn't already exist! Index() returns an items index within a collection, its that simple!

Before this method was shipped, in order to get an items index within a loop, you would write code like this:

This code can now be simplified using Index() like this:

MinBy() and MaxBy()

Both of these methods were released within C# 10 when .NET 6 shipped. These methods will allow you to quickly and easily find the lowest and highest values contained within a list. For example, in a shopping cart MinBy() would find the cheapest item and MaxBy() would return them most expensive item. The code to implement MinBy() is shown below:

By swapping the call to MaxBy() you can get the highest value returned instead!

Distinct()

Distinct() was another C# 10 and .NET 6 feature and you can probably guess what Distinct() does. Distinct() returns a distinct list of elements. It will remove any extra duplicate elements within a list. Going back to the shipping cart, if you wanted to render the cart without iPhone appearing twice, this is the call you would use!

ExceptBy()

The final method in todays list is ExceptBy(). Again, if it helps your memory its worth mentally grouping Distinct() and ExceptBy() together. The difference is simple, Distinct() will work on itself, while ExceptBy() will allow you to provide an additional list that you would to use to remove elements.

In this example, the original list had an entry called Laptop. By adding a filter of Laptop within the second collection, it gets completely removed from the end result. Handy!


Happy Coding 🤘