語言

Enumerable.MinBy 方法

定義

多載

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

來源:
Min.cs
來源:
Min.cs
來源:
Min.cs
來源:
Min.cs
來源:
Min.cs

根據指定的索引鍵選取器函式,傳回泛型序列中的最小值。

public:
generic <typename TSource, typename TKey>
[System::Runtime::CompilerServices::Extension]
 static TSource MinBy(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, TKey> ^ keySelector);
public static TSource? MinBy<TSource,TKey>(this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector);
static member MinBy : seq<'Source> * Func<'Source, 'Key> -> 'Source
<Extension()>
Public Function MinBy(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 or 介面擷取的金鑰都無法實現IComparable。IComparable<T>

TSource 是原始型態,且來源序列為空。

範例

以下程式碼範例示範如何根據 MinBy 特定屬性在集合中尋找最小值。

(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 youngest employee in the company.
var youngestEmployee = employees.MinBy(employee => employee.Age);

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

/*
This code produces the following output:

Name: Mahmoud, Age: 22, Salary: $1000
*/
</格式>

備註

若來源序列為空,根據來源類型有兩種可能結果。 若 TSource 為可空型,此方法回傳 null。 若 TSource 為不可空的結構體,例如原始型態,則拋出 an InvalidOperationException 。

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

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

適用於

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

來源:
Min.cs
來源:
Min.cs
來源:
Min.cs
來源:
Min.cs
來源:
Min.cs

根據指定的索引鍵選取器函式和索引鍵比較子,傳回泛型序列中的最小值。

public:
generic <typename TSource, typename TKey>
[System::Runtime::CompilerServices::Extension]
 static TSource MinBy(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, TKey> ^ keySelector, System::Collections::Generic::IComparer<TKey> ^ comparer);
public static TSource? MinBy<TSource,TKey>(this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector, System.Collections.Generic.IComparer<TKey>? comparer);
static member MinBy : seq<'Source> * Func<'Source, 'Key> * System.Collections.Generic.IComparer<'Key> -> 'Source
<Extension()>
Public Function MinBy(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 or 介面擷取的金鑰都無法實現IComparable。IComparable<T>

TSource 是原始型態,且來源序列為空。

範例

以下程式碼範例示範如何 MinBy 利用自訂比較器忽略大小寫敏感性,以檢查最小值。

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

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

/*
This code produces the following output:

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

備註

若來源序列為空,根據來源類型有兩種可能結果。 若 TSource 為可空型,此方法回傳 null。 若 TSource 為不可空的結構體,例如原始型態,則拋出 an InvalidOperationException 。

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

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

適用於