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
Create data for students with the above properties
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?
![Demo]()
How do I find a top-performing student from the list of students in Net 6 and the latest version using MaxBy()?
![.NET Linq]()
How do I find a poor-performing student from the list of students before .Net 6?
![Lowest]()
How do I find the poor-performing student from the list of students in Net 6 and the latest version using MinBy()?
![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.
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
- Difference Between IQueryable, IEnumerable, And IList In LINQ
- LINQ Extension Methods - Part One
- LINQ Extension Methods Quantifier And Aggregation Operator - Part Two
- LINQ Extension Methods - Element Operator And Set Operator - Part Three
- LINQ Extension Methods - Partitioning Operator - Part Four