Enumerable.MaxBy 方法

定義

多載

名稱 Description
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>)

來源:
Max.cs
來源:
Max.cs
來源:
Max.cs
來源:
Max.cs
來源:
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

序列中鍵數最大值。

例外狀況

sourcenull

source or 介面擷取的金鑰都無法實現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。 若 TSource 為不可空的結構體,例如原始型態,則拋出 an InvalidOperationException

若來源序列僅包含為 null的值,此方法回傳 null

<格式類型=“text/Markdown”>

適用於

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

來源:
Max.cs
來源:
Max.cs
來源:
Max.cs
來源:
Max.cs
來源:
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

序列中鍵數最大值。

例外狀況

sourcenull

source or 介面擷取的金鑰都無法實現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。 若 TSource 為不可空的結構體,例如原始型態,則拋出 an InvalidOperationException

若來源序列僅包含為 null的值,此方法回傳 null

<格式類型=“text/Markdown”>

適用於