Method CountBy
| Edit this pageCountBy<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>)
Applies a key-generating function to each element of a sequence and returns a sequence of unique keys and their number of occurrences in the original sequence.
Declaration
public static IEnumerable<KeyValuePair<TKey, int>> CountBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<TSource> | source | Source sequence. |
| Func<TSource, TKey> | keySelector | Function that transforms each item of source sequence into a key to be compared against the others. |
Returns
| Type | Description |
|---|---|
| IEnumerable<KeyValuePair<TKey, int>> | A sequence of unique keys and their number of occurrences in the original sequence. |
Type Parameters
| Name | Description |
|---|---|
| TSource | Type of the elements of the source sequence. |
| TKey | Type of the projected element. |
Remarks
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 count by key in a sequence, using the CountBy operator:
var sequence = Enumerable.Range(1, 19);
// Count elements in a sequence grouped by key
var result = sequence.CountBy(x => x % 3);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [(1, 7), (2, 6), (0, 6)]
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
CountBy<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>, IEqualityComparer<TKey>?)
Applies a key-generating function to each element of a sequence and returns a sequence of unique keys and their number of occurrences in the original sequence. An additional argument specifies a comparer to use for testing equivalence of keys.
Declaration
public static IEnumerable<KeyValuePair<TKey, int>> CountBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<TSource> | source | Source sequence. |
| Func<TSource, TKey> | keySelector | Function that transforms each item of source sequence into a key to be compared against the others. |
| IEqualityComparer<TKey> | comparer | The equality comparer to use to determine whether or not keys are equal. If null, the default equality
comparer for |
Returns
| Type | Description |
|---|---|
| IEnumerable<KeyValuePair<TKey, int>> | A sequence of unique keys and their number of occurrences in the original sequence. |
Type Parameters
| Name | Description |
|---|---|
| TSource | Type of the elements of the source sequence. |
| TKey | Type of the projected element. |
Remarks
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 count by key in a sequence, using the CountBy operator:
var sequence = new[] { "a", "B", "c", "A", "b", "A", };
// Count elements in a sequence grouped by key
var result = sequence.CountBy(SuperEnumerable.Identity, StringComparer.OrdinalIgnoreCase);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [(a, 3), (B, 2), (c, 1)]
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|