Ler en inglés Editar

Compartir por


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

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

Returns the minimum value in a generic sequence according to a specified key selector function.

C#
public static TSource? MinBy<TSource,TKey>(this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector);

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

TSource

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.

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.

Applies to

.NET 10 e outras versións
Produto Versións
.NET 6, 7, 8, 9, 10

MinBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>, IComparer<TKey>)

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.

C#
public static TSource? MinBy<TSource,TKey>(this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector, System.Collections.Generic.IComparer<TKey>? comparer);

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

TSource

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.

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.

Applies to

.NET 10 e outras versións
Produto Versións
.NET 6, 7, 8, 9, 10