DistinctBy - Kalkwst/Risotto GitHub Wiki
Returns all distinct elements of the given source, where distinctiveness is determined by a transformation and an equality comparer for the transformed type.
In this page
- Definition
- Overloads
- DistinctBy(Func<TSource, TKey> transformer)
- DistinctBy(Func<TSource, TKey> transformer, IEqualityComparer? comparer)
Overload | Description |
---|---|
DistinctBy(Func<TSource, TKey> transformer) | Returns all distinct elements of the given source |
DistinctBy(Func<TSource, TKey> transformer, IEqualityComparer? comparer) | Returns all distinct elements of the given source, using the provided equality comparer |
Returns all distinct elements of the given source, where distinctiveness is determined by a transformation and the default equality comparer for the transformed type.
public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> transformer);
TSource
The type of the elements of the original sequences.
TKey
The type of the projected element.
source
IEnumerable <TSource>
The source sequence.
transformer
Func <TSource, TKey>
A transform function to apply to each element of the sequence.
IEnumerable<TResult>
A sequence consisting of distinct elements from the source, comparing them by the specified key transformation.
ArgumentNullException
source
is null
-or-
transformer
is null
The following code example demonstrates how to use DistinctBy(Func<TSource, TKey> transformer) to determine the distinct elements of a sequence.
string[] source = { "cat", "dog", "pig", "duck", "squid", "bird", "lamb", "frog", "whale" };
var distinct = source.DistinctBy(word => word.Length);
//=> [ "cat", "duck", "squid" ]
This operation uses deferred execution and streams the results, although a set of already-seen keys is retained. If a key is seen multiple times, only the first element with that key is returned.
Returns all distinct elements of the given source, where distinctiveness is determined by a transformation and the specified comparer for the transformed type.
public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> transformer, IEqualityComparer<TKey>? comparer);
TSource
The type of the elements of the original sequences.
TKey
The type of the projected element.
source
IEnumerable <TSource>
The source sequence.
transformer
Func <TSource, TKey>
A transform function to apply to each element of the sequence.
comparer
IEqualityComparer
An equality comparer to compare values.
IEnumerable<TResult>
A sequence consisting of distinct elements from the source, comparing them by the specified key transformation.
ArgumentNullException
source
is null
-or-
transformer
is null
he following code example demonstrates how to use DistinctBy(Func<TSource, TKey> transformer, IEqualityComparer? comparer) to determine the distinct elements of a sequence. Distinctiveness is determined by the provided equality comparer.
string[] source = { "dog", "DOG", "duck", "duck", "whale" };
var distinct = source.DistinctBy(word => word, StringComparer.OrdinalIgnoreCase);
//=> [ "dog", "duck", "whale" ]
This operation uses deferred execution and streams the results, although a set of already-seen keys is retained. If a key is seen multiple times, only the first element with that key is returned.