Enumerable.DistinctBy Method

Definition

Overloads

DistinctBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>)

Returns distinct elements from a sequence according to a specified key selector function.

DistinctBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>, IEqualityComparer<TKey>)

Returns distinct elements from a sequence according to a specified key selector function and using a specified comparer to compare keys.

DistinctBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>)

Source:
Distinct.cs
Source:
Distinct.cs
Source:
Distinct.cs

Returns distinct elements from a sequence according to a specified key selector function.

C#
public static System.Collections.Generic.IEnumerable<TSource> DistinctBy<TSource,TKey>(this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector);

Type Parameters

TSource

The type of the elements of source.

TKey

The type of key to distinguish elements by.

Parameters

source
IEnumerable<TSource>

The sequence to remove duplicate elements from.

keySelector
Func<TSource,TKey>

A function to extract the key for each element.

Returns

IEnumerable<TSource>

An IEnumerable<T> that contains distinct elements from the source sequence.

Exceptions

source is null.

Remarks

This method is implemented by using deferred execution. The immediate return value is an object that stores all the information that is required to perform the action. The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using foreach in C# or For Each in Visual Basic.

The DistinctBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>) method returns an unordered sequence that contains no duplicate values. The default equality comparer, Default, is used to compare values.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET 6, 7, 8, 9

DistinctBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>, IEqualityComparer<TKey>)

Source:
Distinct.cs
Source:
Distinct.cs
Source:
Distinct.cs

Returns distinct elements from a sequence according to a specified key selector function and using a specified comparer to compare keys.

C#
public static System.Collections.Generic.IEnumerable<TSource> DistinctBy<TSource,TKey>(this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector, System.Collections.Generic.IEqualityComparer<TKey>? comparer);

Type Parameters

TSource

The type of the elements of source.

TKey

The type of key to distinguish elements by.

Parameters

source
IEnumerable<TSource>

The sequence to remove duplicate elements from.

keySelector
Func<TSource,TKey>

A function to extract the key for each element.

comparer
IEqualityComparer<TKey>

An IEqualityComparer<T> to compare keys.

Returns

IEnumerable<TSource>

An IEnumerable<T> that contains distinct elements from the source sequence.

Exceptions

source is null.

Remarks

This method is implemented by using deferred execution. The immediate return value is an object that stores all the information that is required to perform the action. The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using foreach in C# or For Each in Visual Basic.

The DistinctBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>, IEqualityComparer<TKey>) method returns an unordered sequence that contains no duplicate values. If comparer is null, the default equality comparer, Default, is used to compare values.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET 6, 7, 8, 9