Queryable.DistinctBy Method

Definition

Overloads

DistinctBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>)

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

DistinctBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, IEqualityComparer<TKey>)

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

DistinctBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>)

Source:
Queryable.cs
Source:
Queryable.cs
Source:
Queryable.cs

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

C#
public static System.Linq.IQueryable<TSource> DistinctBy<TSource,TKey>(this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<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
IQueryable<TSource>

The sequence to remove duplicate elements from.

keySelector
Expression<Func<TSource,TKey>>

A function to extract the key for each element.

Returns

IQueryable<TSource>

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

Exceptions

source is null.

Applies to

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

DistinctBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, IEqualityComparer<TKey>)

Source:
Queryable.cs
Source:
Queryable.cs
Source:
Queryable.cs

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

C#
public static System.Linq.IQueryable<TSource> DistinctBy<TSource,TKey>(this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<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
IQueryable<TSource>

The sequence to remove duplicate elements from.

keySelector
Expression<Func<TSource,TKey>>

A function to extract the key for each element.

comparer
IEqualityComparer<TKey>

An IEqualityComparer<T> to compare keys.

Returns

IQueryable<TSource>

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

Exceptions

source is null.

Applies to

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