Enumerable.MaxBy 方法

定义

重载

名称 说明
MaxBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>, IComparer<TKey>)

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

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

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

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

Source:
Max.cs
Source:
Max.cs
Source:
Max.cs
Source:
Max.cs
Source:
Max.cs

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

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

序列中具有最大键的值。

例外

source null

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

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

示例

下面的代码示例演示如何在检查最大值时与自定义比较器一 MaxBy 起使用,以忽略大小写敏感度。

(string Name, int Quantity)[] inventory =
{
    ("apple", 10),
    ("BANANA", 5),
    ("Cherry", 20)
};

// Find the product with the maximum name alphabetically, ignoring casing differences.
// 'C' is correctly identified as greater than 'a' and 'B' when case is ignored.
var maxIgnoreCase = inventory.MaxBy(item => item.Name, StringComparer.OrdinalIgnoreCase);
Console.WriteLine($"Case-insensitive comparison: {maxIgnoreCase.Name}");

/*
This code produces the following output:

Case-insensitive comparison: Cherry
*/
</格式>

注解

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

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

<format type=“text/markdown”>

适用于

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

Source:
Max.cs
Source:
Max.cs
Source:
Max.cs
Source:
Max.cs
Source:
Max.cs

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

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

序列中具有最大键的值。

例外

source null

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

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

示例

下面的代码示例演示如何根据 MaxBy 特定属性查找集合中的最大值。

(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 oldest employee in the company.
var oldestEmployee = employees.MaxBy(employee => employee.Age);

Console.WriteLine($"Name: {oldestEmployee.Name}, Age: {oldestEmployee.Age}, Salary: ${oldestEmployee.Salary}");

/*
This code produces the following output:

Name: Luna, Age: 33, Salary: $1850
*/
</格式>

注解

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

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

<format type=“text/markdown”>

适用于