Method MinItemsBy
| Edit this page View SourceMinItemsBy<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>)
Returns all of the items that share the minimum value of a sequence.
Declaration
public static IEnumerable<TSource> MinItemsBy<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 minimum 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 Ascending 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 minimum items from a sequence using MinItemsBy
.
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
.MinItemsBy(x => x.cnt);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [(1, A), (1, a)]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
MinItemsBy<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>, IComparer<TKey>?)
Returns all of the items that share the minimum value of a sequence.
Declaration
public static IEnumerable<TSource> MinItemsBy<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 minimum 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 Ascending 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 minimum items from a sequence using MinItemsBy
.
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
.MinItemsBy(x => x.item, StringComparer.OrdinalIgnoreCase);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [(1, A), (1, a)]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|