Exploring .NET 6's MinBy() and MaxBy() Extension Methods

In this article, I will demonstrate the exciting new additions to LINQ extension methods introduced in.NET 6: MinBy() and MaxBy(). These methods offer a streamlined approach to extracting the minimum or maximum value in a generic sequence according to a specified key selector function. Let's explore how these powerful tools enhance data manipulation and simplify code, providing efficient solutions for selecting extreme values within collections.

.NET 6: LINQ EXTENSION METHODS MinBY() AND MaxBY()

Linq Extension methods MinBy and MaxBy

Let's create a record type for students and create a list of students to check MinBy() and MaxBy() in Linq,

Create a record for the student

public record Student(int Id, string Name, DateTime JoiningDate, int Marks);

Create data for students with the above properties

var students = new List<Student>()
{
    new Student(1, "Jignesh", new DateTime(2022, 3, 15), 500),
    new Student(2, "Dharmik", new DateTime(2022, 3, 12), 550),
    new Student(3, "Priyal", new DateTime(2021, 3, 10), 299),
    new Student(4, "Rohit", new DateTime(2022, 4, 18), 535),
     new Student(4, "Tilak", new DateTime(2022, 4, 18), 300)
};
foreach (var item in students)
{
    Console.WriteLine($"Id :{ item.Id}  |  Name :  { item.Name} | Joing Date : {item.JoiningDate.ToShortDateString()} | Marks : {item.Marks}");
}

Let's check and print all students to see whether data was created or not for the student list

Check And print

Before the introduction of MinBy() and MaxBy() in .NET 6, to get the minimum or maximum records from a list involved slightly diffent aproach. Typically, developers would resort to methods like OrderBy() or OrderByDescending() followed by First() or Last() to achieve similar results. With the introduction of new extension methods MinBy() and MaxBy() in .NET 6, the process becomes more elegant and efficient. These new LINQ extension methods have the provision of extracting the minimum or maximum record from a collection based on a specified key

How do I find a top-performing student from the list of students before .Net 6?

var topScorer = students.OrderByDescending(x => x.Marks).First();
Console.WriteLine($"Top Scorer in the class :  Id : {topScorer.Id} Name : {topScorer.Name}");

Demo

How do I find a top-performing student from the list of students in Net 6 and the latest version using MaxBy()?

var topScorer = students.MaxBy(x => x.Marks);

Console.WriteLine($"Top Scorer in the class :  Id : {topScorer?.Id} Name : {topScorer?.Name} Who scrored {topScorer?.Marks} marks");

.NET Linq

How do I find a poor-performing student from the list of students before .Net 6?

var poorPerfomer = students.OrderBy(x => x.Marks).First();

Console.WriteLine($"Lowest Score in the class :  Id : {topScorer?.Id} Name : {topScorer?.Name} Who scrored {topScorer?.Marks} marks");

Lowest

How do I find the poor-performing student from the list of students in Net 6 and the latest version using MinBy()?

var poorPerfomer = students.MinBy(x => x.Marks);

Console.WriteLine($"Lowest Score in the class :  Id : {poorPerfomer?.Id} Name : {poorPerfomer?.Name} Who scrored {poorPerfomer?.Marks} marks");

Linq

Tie Breaker will return the first match value in a generic sequence according to a specified key selector function.

Check the breaker for MaxBy()

Let's check the breaker for MaxBy(). Let me add one student with the same marks (550); that makes the tie breaker for top scorers. In this scenario, the MaxBy() method will return the first match records from the generic sequence.

var students = new List<Student>()
{
    new Student(1, "Jignesh", new DateTime(2022, 3, 15), 500),
    new Student(2, "Dharmik", new DateTime(2022, 3, 12), 550),
    new Student(3, "Priyal", new DateTime(2021, 3, 10), 299),
    new Student(4, "Rohit", new DateTime(2022, 4, 18), 535),
    new Student(4, "Tilak", new DateTime(2022, 4, 18), 300),
    new Student(4, "Jaymin", new DateTime(2022, 4, 18), 550)

};

Still, we will get the same result for the top scorer because we have two students with marks of 550 (Dharmik and Jaymin). MayBy() will return first-match values from a generic sequence based on specified criteria.

Top Score

Summary

In this article, we have learned two of the most useful methods that help developers find the minimum or maximum value in a generic sequence according to a specified key selector function. MinBy() and MaxBy() offer a more efficient way of retrieving extreme values from a list of records, eliminating the need for manual sorting and reducing code complexity.

Thank you for reading. If you wish to read more articles on Linq, please click here. Click here for more articles on Linq

  1. Difference Between IQueryable, IEnumerable, And IList In LINQ
  2. LINQ Extension Methods - Part One
  3. LINQ Extension Methods Quantifier And Aggregation Operator - Part Two
  4. LINQ Extension Methods - Element Operator And Set Operator - Part Three
  5. LINQ Extension Methods - Partitioning Operator - Part Four


Similar Articles