Enumerable.MaxBy メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
オーバーロード
| 名前 | 説明 |
|---|---|
| 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> 。
返品
シーケンス内の最大キーを持つ値。
例外
source は nullです。
IComparableまたはIComparable<T> インターフェイスを実装sourceから抽出されたキーはありません。
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
*/
</形式>
注釈
ソース シーケンスが空の場合は、ソースの種類に応じて 2 つの結果が可能です。
TSourceが null 許容型の場合、このメソッドはnullを返します。
TSourceがプリミティブ型などの null 非許容構造体である場合は、InvalidOperationExceptionがスローされます。
ソース シーケンスに null値のみが含まれている場合、このメソッドは nullを返します。
<format type="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>
各要素のキーを抽出する関数。
返品
シーケンス内の最大キーを持つ値。
例外
source は nullです。
IComparableまたはIComparable<T> インターフェイスを実装sourceから抽出されたキーはありません。
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
*/
</形式>
注釈
ソース シーケンスが空の場合は、ソースの種類に応じて 2 つの結果が可能です。
TSourceが null 許容型の場合、このメソッドはnullを返します。
TSourceがプリミティブ型などの null 非許容構造体である場合は、InvalidOperationExceptionがスローされます。
ソース シーケンスに null値のみが含まれている場合、このメソッドは nullを返します。
<format type="text/markdown">