MaxBy / MinBy In .NET 6

This post is part of a series on .NET 6 and C# 10 features. Use the following links to navigate to other articles in the series and build up your .NET 6/C# 10 knowledge! While the articles are seperated into .NET 6 and C# 10 changes, these days the lines are very blurred so don’t read too much into it.

.NET 6

Minimal API Framework
DateOnly and TimeOnly Types
LINQ OrDefault Enhancements
Implicit Using Statements
IEnumerable Chunk
SOCKS Proxy Support
Priority Queue
MaxBy/MinBy

C# 10

Global Using Statements
File Scoped Namespaces


Reading through the new features introduced in .NET 6, something that I repeatedly glossed over was the new MaxBy and MinBy LINQ statements. I kept glossing over them because at first look, it doesn’t seem to be anything new but in reality.. They are actually extremely helpful!

Getting Setup With .NET 6 Preview

At the time of writing, .NET 6 is in preview, and is not currently available in general release. That doesn’t mean it’s hard to set up, it just means that generally you’re not going to have it already installed on your machine if you haven’t already been playing with some of the latest fandangle features.

To get set up using .NET 6, you can go and read out guide here : https://dotnetcoretutorials.com/2021/03/13/getting-setup-with-net-6-preview/

Remember, this feature is *only* available in .NET 6. Not .NET 5, not .NET Core 3.1, or any other version you can think of. 6.

Max/MinBy vs Max/Min

So it’s actually brutally simple.

I can run a simple Max statement like so across a list of numeric values :

List<int> myList = new List<int> { 1, 2, 3 };
myList.Max(); //Returns 3

Seems simple, and we output the maximum numeric value.

If I have a complex type, like a class of “Person”, then you might think I need to do something rinky dink to find the max age of those people (as shown below). I actually at first thought this was what “MaxBy” was all about. Like “Max By Property XYZ”. But infact, the regular max statement works just fine like so :

List<Person> people = new List<Person>
{
    new Person
    {
        Name = "John Smith", 
        Age = 20
    }, 
    new Person
    {
        Name = "Jane Smith", 
        Age = 30
    }
};

Console.Write(people.Max(x => x.Age)); //Outputs 30

class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

So what happens if we change that same statement to MaxBy?

List<Person> people = new List<Person>
{
    new Person
    {
        Name = "John Smith", 
        Age = 20
    }, 
    new Person
    {
        Name = "Jane Smith", 
        Age = 30
    }
};

Console.Write(people.MaxBy(x => x.Age)); //Outputs Person (Jane Smith)

Instead of outputting the max property value, we output the entire object that has the max value. So instead of returning “30” we return the entire Person object of Jane Smith.

Another way to look at it, is it’s more or less a shortening of the following statement :

people.OrderByDescending(x => x.Age).First(); //Outputs Person (Jane Smith)

This is the crux of MaxBy/MinBy and is a pretty nifty addition that I totally didn’t know I needed until now.

1 thought on “MaxBy / MinBy In .NET 6”

Leave a Comment