Bewerken

Share via


Enumerable.MaxBy Method

Definition

Overloads

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

Returns the maximum value in a generic sequence according to a specified key selector function.

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

Returns the maximum value in a generic sequence according to a specified key selector function and key comparer.

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

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

Returns the maximum value in a generic sequence according to a specified key selector function.

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

Type Parameters

TSource

The type of the elements of source.

TKey

The type of key to compare elements by.

Parameters

source
IEnumerable<TSource>

A sequence of values to determine the maximum value of.

keySelector
Func<TSource,TKey>

A function to extract the key for each element.

Returns

TSource

The value with the maximum key in the sequence.

Exceptions

source is null.

No key extracted from source implements the IComparable or IComparable<T> interface.

TSource is a primitive type and the source sequence is empty.

Remarks

If the source sequence is empty, two possible outcomes are possible depending on the source type. If TSource is a nullable type, this method returns null. If TSource is a non-nullable struct, such as a primitive type, an InvalidOperationException is thrown.

If the source sequence contains only values that are null, this method returns null.

Applies to

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

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

Returns the maximum value in a generic sequence according to a specified key selector function and key comparer.

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

Type Parameters

TSource

The type of the elements of source.

TKey

The type of key to compare elements by.

Parameters

source
IEnumerable<TSource>

A sequence of values to determine the maximum value of.

keySelector
Func<TSource,TKey>

A function to extract the key for each element.

comparer
IComparer<TKey>

The IComparer<T> to compare keys.

Returns

TSource

The value with the maximum key in the sequence.

Exceptions

source is null.

No key extracted from source implements the IComparable or IComparable<T> interface.

TSource is a primitive type and the source sequence is empty.

Remarks

If the source sequence is empty, two possible outcomes are possible depending on the source type. If TSource is a nullable type, this method returns null. If TSource is a non-nullable struct, such as a primitive type, an InvalidOperationException is thrown.

If the source sequence contains only values that are null, this method returns null.

Applies to