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>)

ソース:
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

シーケンス内の最小キーを持つ値。

例外

sourcenullです。

IComparableまたはIComparable<T> インターフェイスを実装sourceから抽出されたキーはありません。

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
*/
</形式>

注釈

ソース シーケンスが空の場合は、ソースの種類に応じて 2 つの結果が可能です。 TSourceが null 許容型の場合、このメソッドはnullを返します。 TSourceがプリミティブ型などの null 非許容構造体である場合は、InvalidOperationExceptionがスローされます。

ソース シーケンスに null値のみが含まれている場合、このメソッドは nullを返します。

<format type="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

シーケンス内の最小キーを持つ値。

例外

sourcenullです。

IComparableまたはIComparable<T> インターフェイスを実装sourceから抽出されたキーはありません。

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
*/
</形式>

注釈

ソース シーケンスが空の場合は、ソースの種類に応じて 2 つの結果が可能です。 TSourceが null 許容型の場合、このメソッドはnullを返します。 TSourceがプリミティブ型などの null 非許容構造体である場合は、InvalidOperationExceptionがスローされます。

ソース シーケンスに null値のみが含まれている場合、このメソッドは nullを返します。

<format type="text/markdown">

適用対象