Queryable.Distinct Method

Definition

Returns distinct elements from a sequence.

Overloads

Distinct<TSource>(IQueryable<TSource>)

Returns distinct elements from a sequence by using the default equality comparer to compare values.

Distinct<TSource>(IQueryable<TSource>, IEqualityComparer<TSource>)

Returns distinct elements from a sequence by using a specified IEqualityComparer<T> to compare values.

Distinct<TSource>(IQueryable<TSource>)

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

Returns distinct elements from a sequence by using the default equality comparer to compare values.

C#
public static System.Linq.IQueryable<TSource> Distinct<TSource>(this System.Linq.IQueryable<TSource> source);

Type Parameters

TSource

The type of the elements of source.

Parameters

source
IQueryable<TSource>

The IQueryable<T> to remove duplicates from.

Returns

IQueryable<TSource>

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

Exceptions

source is null.

Examples

The following code example demonstrates how to use Distinct<TSource>(IQueryable<TSource>) to return distinct elements from a sequence.

C#
List<int> ages = new List<int> { 21, 46, 46, 55, 17, 21, 55, 55 };

IEnumerable<int> distinctAges = ages.AsQueryable().Distinct();

Console.WriteLine("Distinct ages:");

foreach (int age in distinctAges)
    Console.WriteLine(age);

/*
    This code produces the following output:

    Distinct ages:
    21
    46
    55
    17
*/

Remarks

The Distinct<TSource>(IQueryable<TSource>) method generates a MethodCallExpression that represents calling Distinct<TSource>(IQueryable<TSource>) itself as a constructed generic method. It then passes the MethodCallExpression to the CreateQuery<TElement>(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

The query behavior that occurs as a result of executing an expression tree that represents calling Distinct<TSource>(IQueryable<TSource>) depends on the implementation of the type of the source parameter. The expected behavior is that it returns an unordered sequence of the unique items in source.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

Distinct<TSource>(IQueryable<TSource>, IEqualityComparer<TSource>)

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

Returns distinct elements from a sequence by using a specified IEqualityComparer<T> to compare values.

C#
public static System.Linq.IQueryable<TSource> Distinct<TSource>(this System.Linq.IQueryable<TSource> source, System.Collections.Generic.IEqualityComparer<TSource> comparer);
C#
public static System.Linq.IQueryable<TSource> Distinct<TSource>(this System.Linq.IQueryable<TSource> source, System.Collections.Generic.IEqualityComparer<TSource>? comparer);

Type Parameters

TSource

The type of the elements of source.

Parameters

source
IQueryable<TSource>

The IQueryable<T> to remove duplicates from.

comparer
IEqualityComparer<TSource>

An IEqualityComparer<T> to compare values.

Returns

IQueryable<TSource>

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

Exceptions

source or comparer is null.

Remarks

The Distinct<TSource>(IQueryable<TSource>, IEqualityComparer<TSource>) method generates a MethodCallExpression that represents calling Distinct<TSource>(IQueryable<TSource>, IEqualityComparer<TSource>) itself as a constructed generic method. It then passes the MethodCallExpression to the CreateQuery<TElement>(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

The query behavior that occurs as a result of executing an expression tree that represents calling Distinct<TSource>(IQueryable<TSource>, IEqualityComparer<TSource>) depends on the implementation of the type of the source parameter. The expected behavior is that it returns an unordered sequence of the unique items in source by using comparer to compare values.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0