Method MaxByWithTies
| Edit this page View SourceMaxByWithTies<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>)
Returns all of the items that share the maximum value of a sequence.
Declaration
public static IEnumerable<TSource> MaxByWithTies<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | The source sequence. |
Func<TSource, TKey> | keySelector | A function to extract a key from an element. |
Returns
Type | Description |
---|---|
IEnumerable<TSource> | An IEnumerable<T> containing all of the items that share the maximum value. |
Type Parameters
Name | Description |
---|---|
TSource | Type of elements in the sequence. |
TKey | Type of keys. |
Remarks
This operator is a shortcut for DensePartialSortBy<TSource, TKey>(IEnumerable<TSource>, int, Func<TSource, TKey>, OrderByDirection) with a direction
of Descending and a count
of 1
.
This method is implemented by using deferred execution. However, source
will be consumed
in it's entirety immediately when first element of the returned sequence is consumed.
Examples
The following code example demonstrates how to get the maximum items from a sequence using MaxByWithTies
.
var sequence = new[]
{
(cnt: 1, item: "A"),
(cnt: 2, item: "B"),
(cnt: 3, item: "C"),
(cnt: 4, item: "D"),
(cnt: 5, item: "E"),
(cnt: 1, item: "a"),
(cnt: 2, item: "b"),
(cnt: 3, item: "c"),
(cnt: 4, item: "d"),
(cnt: 5, item: "e"),
}.AsEnumerable();
// Find the maximum items of the sequence
var result = sequence
.MaxByWithTies(x => x.cnt);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [(5, E), (5, e)]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
MaxByWithTies<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>, IComparer<TKey>?)
Returns all of the items that share the maximum value of a sequence.
Declaration
public static IEnumerable<TSource> MaxByWithTies<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey>? comparer)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | The source sequence. |
Func<TSource, TKey> | keySelector | A function to extract a key from an element. |
IComparer<TKey> | comparer | A IComparer<T> to compare keys. |
Returns
Type | Description |
---|---|
IEnumerable<TSource> | An IEnumerable<T> containing all of the items that share the maximum value. |
Type Parameters
Name | Description |
---|---|
TSource | Type of elements in the sequence. |
TKey | Type of keys. |
Remarks
This operator is a shortcut for DensePartialSortBy<TSource, TKey>(IEnumerable<TSource>, int, Func<TSource, TKey>, IComparer<TKey>?, OrderByDirection) with a direction
of Descending and a count
of 1
.
This method is implemented by using deferred execution. However, source
will be consumed
in it's entirety immediately when first element of the returned sequence is consumed.
Examples
The following code example demonstrates how to get the maximum items from a sequence using MaxByWithTies
.
var sequence = new[]
{
(cnt: 1, item: "A"),
(cnt: 2, item: "B"),
(cnt: 3, item: "C"),
(cnt: 4, item: "D"),
(cnt: 5, item: "E"),
(cnt: 1, item: "a"),
(cnt: 2, item: "b"),
(cnt: 3, item: "c"),
(cnt: 4, item: "d"),
(cnt: 5, item: "e"),
}.AsEnumerable();
// Find the maximum items of the sequence
var result = sequence
.MaxByWithTies(x => x.item, StringComparer.OrdinalIgnoreCase);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [(5, E), (5, e)]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|