Enumerable.MinBy Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Overloads
| Name | Description |
|---|---|
| MinBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>) |
Returns the minimum value in a generic sequence according to a specified key selector function. |
| MinBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>, IComparer<TKey>) |
Returns the minimum value in a generic sequence according to a specified key selector function and key comparer. |
MinBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>)
- Source:
- Min.cs
- Source:
- Min.cs
- Source:
- Min.cs
- Source:
- Min.cs
- Source:
- Min.cs
Returns the minimum value in a generic sequence according to a specified key selector function.
public:
generic <typename TSource, typename TKey>
[System::Runtime::CompilerServices::Extension]
static TSource MinBy(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, TKey> ^ keySelector);
public static TSource? MinBy<TSource,TKey>(this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector);
static member MinBy : seq<'Source> * Func<'Source, 'Key> -> 'Source
<Extension()>
Public Function MinBy(Of TSource, TKey) (source As IEnumerable(Of TSource), keySelector As Func(Of TSource, TKey)) As TSource
Type Parameters
- TSource
The type of the elements of source.
- TKey
The type of key to compare elements by.
Parameters
- source
- IEnumerable<TSource>
A sequence of values to determine the minimum value of.
- keySelector
- Func<TSource,TKey>
A function to extract the key for each element.
Returns
The value with the minimum key in the sequence.
Exceptions
source is null.
No key extracted from source implements the IComparable or IComparable<T> interface.
TSource is a primitive type and the source sequence is empty.
Examples
The following code example demonstrates how to use MinBy to find the minimum value in a collection based on a specific property.
(string Name, decimal Salary, int Age)[] employees =
{
("Mahmoud", 1000m, 22),
("John", 1200m, 28),
("Sara", 2000m, 32),
("Hadi", 1750m, 27),
("Lana", 1500m, 24),
("Luna", 1850m, 33)
};
// Get the youngest employee in the company.
var youngestEmployee = employees.MinBy(employee => employee.Age);
Console.WriteLine($"Name: {youngestEmployee.Name}, Age: {youngestEmployee.Age}, Salary: ${youngestEmployee.Salary}");
/*
This code produces the following output:
Name: Mahmoud, Age: 22, Salary: $1000
*/
</format>
Remarks
If the source sequence is empty, two possible outcomes are possible depending on the source type. If TSource is a nullable type, this method returns null. If TSource is a non-nullable struct, such as a primitive type, an InvalidOperationException is thrown.
If the source sequence contains only values that are null, this method returns null.
<format type="text/markdown">
Applies to
MinBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>, IComparer<TKey>)
- Source:
- Min.cs
- Source:
- Min.cs
- Source:
- Min.cs
- Source:
- Min.cs
- Source:
- Min.cs
Returns the minimum value in a generic sequence according to a specified key selector function and key comparer.
public:
generic <typename TSource, typename TKey>
[System::Runtime::CompilerServices::Extension]
static TSource MinBy(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, TKey> ^ keySelector, System::Collections::Generic::IComparer<TKey> ^ comparer);
public static TSource? MinBy<TSource,TKey>(this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector, System.Collections.Generic.IComparer<TKey>? comparer);
static member MinBy : seq<'Source> * Func<'Source, 'Key> * System.Collections.Generic.IComparer<'Key> -> 'Source
<Extension()>
Public Function MinBy(Of TSource, TKey) (source As IEnumerable(Of TSource), keySelector As Func(Of TSource, TKey), comparer As IComparer(Of TKey)) As TSource
Type Parameters
- TSource
The type of the elements of source.
- TKey
The type of key to compare elements by.
Parameters
- source
- IEnumerable<TSource>
A sequence of values to determine the minimum value of.
- keySelector
- Func<TSource,TKey>
A function to extract the key for each element.
- comparer
- IComparer<TKey>
The IComparer<T> to compare keys.
Returns
The value with the minimum key in the sequence.
Exceptions
source is null.
No key extracted from source implements the IComparable or IComparable<T> interface.
TSource is a primitive type and the source sequence is empty.
Examples
The following code example demonstrates how to use MinBy with a custom comparer to ignore case sensitivity when checking for the minimum value.
(string Name, int Quantity)[] inventory =
{
("apple", 10),
("BANANA", 5),
("Cherry", 20)
};
// Find the product with the minimum name alphabetically, ignoring casing differences.
// 'a' is correctly identified as smaller than 'B' when case is ignored.
var minIgnoreCase = inventory.MinBy(item => item.Name, StringComparer.OrdinalIgnoreCase);
Console.WriteLine($"Case-insensitive comparison: {minIgnoreCase.Name}");
/*
This code produces the following output:
Case-insensitive comparison: apple
*/
</format>
Remarks
If the source sequence is empty, two possible outcomes are possible depending on the source type. If TSource is a nullable type, this method returns null. If TSource is a non-nullable struct, such as a primitive type, an InvalidOperationException is thrown.
If the source sequence contains only values that are null, this method returns null.
<format type="text/markdown">