Enumerable.MinBy 方法

定义

重载

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

根据指定的键选择器函数返回泛型序列中的最小值。

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

根据指定的键选择器函数和键比较器返回泛型序列中的最小值。

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

Source:
Min.cs
Source:
Min.cs
Source:
Min.cs

根据指定的键选择器函数返回泛型序列中的最小值。

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

类型参数

TSource

source 的元素类型。

TKey

要比较元素的键的类型。

参数

source
IEnumerable<TSource>

要确定其最小值的值序列。

keySelector
Func<TSource,TKey>

用于提取每个元素的键的函数。

返回

TSource

序列中具有最小键的值。

例外

sourcenull

source 中提取的键不实现 IComparableIComparable<T> 接口。

TSource 是基元类型,源序列为空。

注解

如果源序列为空,则根据源类型,可能会出现两种可能的结果。 如果 TSource 为可以为 null 的类型,则此方法返回 null。 如果 TSource 是不可为空的结构(例如基元类型), InvalidOperationException 则会引发 。

如果源序列仅包含 的值, null则此方法返回 null

适用于

.NET 9 和其他版本
产品 版本
.NET 6, 7, 8, 9

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

Source:
Min.cs
Source:
Min.cs
Source:
Min.cs

根据指定的键选择器函数和键比较器返回泛型序列中的最小值。

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

类型参数

TSource

source 的元素类型。

TKey

要比较元素的键的类型。

参数

source
IEnumerable<TSource>

要确定其最小值的值序列。

keySelector
Func<TSource,TKey>

用于提取每个元素的键的函数。

comparer
IComparer<TKey>

IComparer<T>要比较键的 。

返回

TSource

序列中具有最小键的值。

例外

sourcenull

source 中提取的键不实现 IComparableIComparable<T> 接口。

TSource 是基元类型,源序列为空。

注解

如果源序列为空,则根据源类型,可能会出现两种可能的结果。 如果 TSource 为可以为 null 的类型,则此方法返回 null。 如果 TSource 是不可为空的结构(例如基元类型), InvalidOperationException 则会引发 。

如果源序列仅包含 的值, null则此方法返回 null

适用于

.NET 9 和其他版本
产品 版本
.NET 6, 7, 8, 9