CountBy - Kalkwst/Risotto GitHub Wiki
Apply a key generating function to each element of a sequence and returns a sequence of unique keys and their number of occurences in the original sequence.
In this page
- Definition
- Overloads
- CountBy(Func<TSource, TKey> selector)
- CountBy(Func<TSource, TKey> selector, IEqualityComparer? comparer)
Overload | Description |
---|---|
CountBy(Func<TSource, TKey> selector) | Apply a key generating function on each element of the sequence, and return the count of keys generated |
CountBy(Func<TSource, TKey> selector, IEqualityComparer? comparer) | Apply a key generating function on each element of the sequence, and return the count of keys generated, the equality determined by an optional custom comparer
|
Apply a key generating function to each element of a sequence and returns a sequence of unique keys and their number of occurences in the original sequence.
public static IEnumerable<KeyValuePair<TKey, int>> CountBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> selector);
TSource
The type of the elements of source
source
IEnumerable <TSource>
A sequence to count unique keys based on the provided key generating function.
selector
Func <TSource, TKey>
A key generating function applied to each element of the sequence.
IEnumerable<KeyValuePair<TKey, int>>
A sequence of key-value pairs containing the key generated from the selector
function, and the count of keys as a value.
ArgumentNullException
source
is null
-or-
selector
is null
The following code example demonstrates how to use CountBy(Func<TSource, TKey> selector) to generate keys for each element in the sequence and get the count of unique keys.
var result = new double[] { 6.1, 4.3, 6.12, 3.11, 8.198 }.CountBy(x => Math.Floor(x));
//=> [ { 6, 2 }, { 4, 1 }, { 3, 1 }, { 8, 1 } } ]
The following code example demonstrates the use of CountBy(Func<TSource, TKey> selector) on a string.
var result = "squizzing".CountBy(x => x);
//=> [ { 's', 1 }, { 'q' , 1}, { 'u', 1 }, { 'i', 2 }, { 'z', 2 }, { 'n', 1 }, { 'g', 1} ]
Apply a key generating function to each element of a sequence and returns a sequence of unique keys and their number of occurences in the original sequence. An additional argument specifies a comparer to use for testing equivalence of the keys.
public static IEnumerable<KeyValuePair<TKey, int>> CountBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> selector, IEqualityComparer<TKey>? comparer);
TSource
The type of the elements of source
source
IEnumerable <TSource>
A sequence to count unique keys based on the provided key generating function.
selector
Func <TSource, TKey>
A key generating function applied to each element of the sequence.
comparer
IEqualityComparer
An equality comparer to compare values.
IEnumerable<KeyValuePair<TKey, int>>
A sequence of key-value pairs containing the key generated from the selector
function, and the count of keys as a value.
ArgumentNullException
source
is null
-or-
selector
is null
The following code example demonstrates how to use CountBy(Func<TSource, TKey> selector, IEqualityComparer? comparer) to generate keys for each element in the sequence and get the count of unique keys, using a custom comparer
.
var result = new string[]{ "a", "B", "c", "A", "b", "A" }.CountBy(x => x, StringComparer.OrdinalIgnoreCase);
//=> [ { 'a', 3 }, { 'B', 2 }, { 'c', 1 } ]