Enumerable.MaxBy 方法

定义

重载

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

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

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

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

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

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

public:
generic <typename TSource, typename TKey>
[System::Runtime::CompilerServices::Extension]
 static TSource MaxBy(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, TKey> ^ keySelector);
public static TSource? MaxBy<TSource,TKey> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector);
static member MaxBy : seq<'Source> * Func<'Source, 'Key> -> 'Source
<Extension()>
Public Function MaxBy(Of TSource, TKey) (source As IEnumerable(Of TSource), keySelector As Func(Of TSource, TKey)) As TSource

类型参数

TSource

source 的元素类型。

TKey

要比较元素的键的类型。

参数

source
IEnumerable<TSource>

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

keySelector
Func<TSource,TKey>

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

返回

TSource

序列中具有最大键的值。

例外

sourcenull

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

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

注解

如果源序列为空并且 TSource 是可为空的类型,则此方法返回 null。 如果源序列为空,并且 TSource 是不可为 null 的结构(例如基元类型), InvalidOperationException 则会引发 。

如果源序列仅包含值为 的值,则 null为 。

适用于

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

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

public:
generic <typename TSource, typename TKey>
[System::Runtime::CompilerServices::Extension]
 static TSource MaxBy(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, TKey> ^ keySelector, System::Collections::Generic::IComparer<TKey> ^ comparer);
public static TSource? MaxBy<TSource,TKey> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector, System.Collections.Generic.IComparer<TKey>? comparer);
static member MaxBy : seq<'Source> * Func<'Source, 'Key> * System.Collections.Generic.IComparer<'Key> -> 'Source
<Extension()>
Public Function MaxBy(Of TSource, TKey) (source As IEnumerable(Of TSource), keySelector As Func(Of TSource, TKey), comparer As IComparer(Of TKey)) As TSource

类型参数

TSource

source 的元素类型。

TKey

要比较元素的键的类型。

参数

source
IEnumerable<TSource>

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

keySelector
Func<TSource,TKey>

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

comparer
IComparer<TKey>

IComparer<T>要比较键的 。

返回

TSource

序列中具有最大键的值。

例外

sourcenull

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

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

注解

如果源序列为空并且 TSource 是可为空的类型,则此方法返回 null。 如果源序列为空,并且 TSource 是不可为 null 的结构(例如基元类型), InvalidOperationException 则会引发 。

如果源序列仅包含值为 的值,则 null为 。

适用于