DistinctBy - Kalkwst/Risotto GitHub Wiki

Definition

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

Overloads

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

DistinctBy(Func<TSource, TKey> transformer)

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);

Type Parameters

TSource
The type of the elements of the original sequences.

TKey
The type of the projected element.

Parameters

source IEnumerable <TSource>
The source sequence.

transformer Func <TSource, TKey>
A transform function to apply to each element of the sequence.

Returns

IEnumerable<TResult>
A sequence consisting of distinct elements from the source, comparing them by the specified key transformation.

Exceptions

ArgumentNullException
source is null

-or-

transformer is null

Example

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" ]

Remarks

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.

DistinctBy(Func<TSource, TKey> transformer, IEqualityComparer? comparer)

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);

Type Parameters

TSource
The type of the elements of the original sequences.

TKey
The type of the projected element.

Parameters

source IEnumerable <TSource>
The source sequence.

transformer Func <TSource, TKey>
A transform function to apply to each element of the sequence.

comparerIEqualityComparer
An equality comparer to compare values.

Returns

IEnumerable<TResult>
A sequence consisting of distinct elements from the source, comparing them by the specified key transformation.

Exceptions

ArgumentNullException
source is null

-or-

transformer is null

Example

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" ]

Remarks

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.

⚠️ **GitHub.com Fallback** ⚠️