LINQ Enhancements in .NET 6. Chunk, MaxBy, MinBy, FirstOrDefault and Other

It’s almost November, and release for .NET 6 is on the way. Time to take a look at what was added in the new release if you have not done this yet. I’ll highlight a few handy features that .net 6 brings for us. If you follow the latest updates from .NET Team, you already get these updates in preview4.

enhancements in linq .net 6

MaxBy and MinBy

New methods allow grabbing maximal and minimal elements from a collection using a key selector. It’s quite a standard operation, and every developer has written a code to solve it at least once.

Is it helpful or just new methods that clutter up the list of LINQ methods. Let’s compare it with manual implementation and implementation, which uses LINQ methods.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// MaxBy && MinBy with o(n)
var topPaid = collection.MaxBy(e => e.Salary);
var lessPaid = collection.MinBy(e => e.Salary);
// Manual search gives o(n)
var firstElement = collection.FirstOrDefault();
if (firstElement != null)
{
Employee maxElement = firstElement;
for (var i = 1; i < collection.Count; ++i)
{
if (maxElement.Salary < collection[i].Salary)
{
maxElement = collection[i];
}
}
}

// o(n) is definitely better than any sorting algorithm
var topPaidLinqWay = collection.OrderBy(e => e.Salary).FirstOrDefault();

As you can see, we can get max and min elements in a few ways. Using LINQ and sorting collection by ascending or descending after sorting element, which we are looking for is the first one. No need to dive into sorting, which is used by LINQ to order collection, and it will be worse than just a single iteration. We can manually find the required min or max value in a collection, but as you can see, it takes some time to write the code. It’s pretty convenient that MinBy and MaxBy exist and give an O(n) complexity.

Be aware that finding MaxBy or MinBy on an empty collection will get an error.

Chunk

Another enhancement that will help developers write code is converting the collection into a set of smaller collections with a fixed size. I faced a few chunk implementations in my career, and now it’s a part of LINQ no need to reinvent the wheel each time.

FirstOrDefault, LastOrDefault, SingleOrDefault — now accepts default value

It is a small improvement, and I doubt it is required for reference types, but it is good for value types.

For example, the new improvement allows passing default values directly into the method.

1
2
3
var defaultValue = new Employee();
var emptyCollection = new List<Employee>();
var firstEmployee = emptyCollection.FirstOrDefault(defaultValue);

But it could be solved in a different way for nullable types

1
2
3
var defaultValue = new Employee();
var emptyCollection = new List<Employee>();
var firstEmployee = emptyCollection.FirstOrDefault() ?? defaultValue;

Conclusion

In the new release, we get a few minor improvements for LINQ. Some of them are pretty convenient for usage in daily coding, and I suggest you don’t hesitate and try them by yourself.