MinBy() and MaxBy() Extension Methods in .NET

By FoxLearn 2/22/2025 4:12:07 AM   5
In this article, we will explore two exciting new LINQ extension methods introduced in .NET 6: MinBy() and MaxBy(). These methods provide a more streamlined and efficient way to find the minimum or maximum value in a sequence, based on a key selector function.

LINQ Extension Methods MinBy() and MaxBy() in .NET 6

Let's start by defining a Product record and creating a list of products to demonstrate MinBy() and MaxBy().

Define a Product Record:

public record Product(int Id, string Name, DateTime ReleaseDate, decimal Price);

Create Product Data:

var products = new List<Product>()
{
    new Product(1, "Laptop", new DateTime(2022, 5, 10), 1200.50),
    new Product(2, "Smartphone", new DateTime(2022, 2, 25), 799.99),
    new Product(3, "Tablet", new DateTime(2021, 9, 15), 499.49),
    new Product(4, "Smartwatch", new DateTime(2022, 8, 1), 199.99),
    new Product(5, "Headphones", new DateTime(2022, 6, 20), 150.00)
};

Print the List of Products:

foreach (var product in products)
{
    Console.WriteLine($"Id: {product.Id} | Name: {product.Name} | Release Date: {product.ReleaseDate.ToShortDateString()} | Price: {product.Price:C}");
}

Finding the Most Expensive Product (Before .NET 6)

Before MinBy() and MaxBy() were introduced, you would typically use OrderByDescending() to get the most expensive product from a list:

var mostExpensiveProduct = products.OrderByDescending(x => x.Price).First();
Console.WriteLine($"Most Expensive Product: Id: {mostExpensiveProduct.Id} | Name: {mostExpensiveProduct.Name} | Price: {mostExpensiveProduct.Price:C}");

Finding the Most Expensive Product in .NET 6 Using MaxBy()

With .NET 6, you can now use MaxBy() to achieve this more concisely:

var mostExpensiveProduct = products.MaxBy(x => x.Price);
Console.WriteLine($"Most Expensive Product: Id: {mostExpensiveProduct?.Id} | Name: {mostExpensiveProduct?.Name} | Price: {mostExpensiveProduct?.Price:C}");

Finding the Least Expensive Product (Before .NET 6)

Similarly, before .NET 6, finding the least expensive product would involve sorting the list in ascending order:

var leastExpensiveProduct = products.OrderBy(x => x.Price).First();
Console.WriteLine($"Least Expensive Product: Id: {leastExpensiveProduct?.Id} | Name: {leastExpensiveProduct?.Name} | Price: {leastExpensiveProduct?.Price:C}");

Finding the Least Expensive Product in .NET 6 Using MinBy()

With MinBy() in .NET 6, you can find the least expensive product more efficiently:

var leastExpensiveProduct = products.MinBy(x => x.Price);
Console.WriteLine($"Least Expensive Product: Id: {leastExpensiveProduct?.Id} | Name: {leastExpensiveProduct?.Name} | Price: {leastExpensiveProduct?.Price:C}");

Handling Ties with MaxBy() and MinBy()

If there is a tie (i.e., two products have the same price), MaxBy() and MinBy() will return the first match based on the order of the sequence.

var productsWithTie = new List<Product>()
{
    new Product(1, "Laptop", new DateTime(2022, 5, 10), 1200.50),
    new Product(2, "Smartphone", new DateTime(2022, 2, 25), 799.99),
    new Product(3, "Tablet", new DateTime(2021, 9, 15), 1200.50), // Same price as Laptop
    new Product(4, "Smartwatch", new DateTime(2022, 8, 1), 199.99),
    new Product(5, "Headphones", new DateTime(2022, 6, 20), 150.00)
};

var mostExpensiveProduct = productsWithTie.MaxBy(x => x.Price);
Console.WriteLine($"Most Expensive Product (Tie Breaker): Id: {mostExpensiveProduct?.Id} | Name: {mostExpensiveProduct?.Name} | Price: {mostExpensiveProduct?.Price:C}");

In this case, even though both the "Laptop" and "Tablet" have the same price, MaxBy() will return the first match, which is the "Laptop".

In this article, we explored the MinBy() and MaxBy() methods introduced in .NET 6, which provide more efficient and elegant solutions for selecting the minimum and maximum values based on a key.

By using MinBy() and MaxBy(), you can improve the readability and performance of your LINQ queries when working with collections of records.