Returns the maximal element of the given sequence, based on
the given projection and the specified comparer for projected values.
Declaration Syntax
C# | Visual Basic | Visual C++ |
public static TSource MaxBy<TSource, TKey>( this IEnumerable<TSource> source, Func<TSource, TKey> selector, IComparer<TKey> comparer )
<ExtensionAttribute> _ Public Shared Function MaxBy(Of TSource, TKey) ( _ source As IEnumerable(Of TSource), _ selector As Func(Of TSource, TKey), _ comparer As IComparer(Of TKey) _ ) As TSource
public: [ExtensionAttribute] generic<typename TSource, typename TKey> static TSource MaxBy( IEnumerable<TSource>^ source, Func<TSource, TKey>^ selector, IComparer<TKey>^ comparer )
Generic Template Parameters
- TSource
- Type of the source sequence
- TKey
- Type of the projected element
Parameters
- source (IEnumerable<(Of <(<'TSource>)>)>)
- Source sequence
- selector (Func<(Of <(<'TSource, TKey>)>)>)
- Selector to use to pick the results to compare
- comparer (IComparer<(Of <(<'TKey>)>)>)
- Comparer to use to compare projected values
Return Value
The maximal element, according to the projection.
Usage Note
In Visual Basic and C#, you can call this method as an instance method on any object of type IEnumerable<(Of <(<'TSource>)>)>. When you use instance method syntax to call this method, omit the first parameter.
Remarks
If more than one element has the maximal projected value, the first
one encountered will be returned. This overload uses the default comparer
for the projected type. This operator uses immediate execution, but
only buffers a single result (the current maximal element).
Exceptions
Exception | Condition |
---|---|
ArgumentNullException | source, selector
or comparer is null |
InvalidOperationException | source is empty |
Assembly: MoreLinq (Module: MoreLinq.dll) Version: 1.0.16006.0 (1.0.16006.1845)