Language

Queryable.Distinct Method

Definition

Returns distinct elements from a sequence.

Overloads

Name Description
Distinct(IQueryable, IEqualityComparer)

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

Distinct(IQueryable)

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

Distinct(IQueryable, IEqualityComparer)

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

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

public:
generic 
[System::Runtime::CompilerServices::Extension]
 static System::Linq::IQueryable ^ Distinct(System::Linq::IQueryable ^ source, System::Collections::Generic::IEqualityComparer ^ comparer);
public static System.Linq.IQueryable Distinct(this System.Linq.IQueryable source, System.Collections.Generic.IEqualityComparer comparer);
public static System.Linq.IQueryable Distinct(this System.Linq.IQueryable source, System.Collections.Generic.IEqualityComparer? comparer);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")]
public static System.Linq.IQueryable Distinct(this System.Linq.IQueryable source, System.Collections.Generic.IEqualityComparer? comparer);
static member Distinct : System.Linq.IQueryable<'Source> * System.Collections.Generic.IEqualityComparer<'Source> -> System.Linq.IQueryable<'Source>
[]
static member Distinct : System.Linq.IQueryable<'Source> * System.Collections.Generic.IEqualityComparer<'Source> -> System.Linq.IQueryable<'Source>

Public Function Distinct(Of TSource) (source As IQueryable(Of TSource), comparer As IEqualityComparer(Of TSource)) As IQueryable(Of TSource)

Type Parameters

TSource

The type of the elements of source.

Parameters

source
IQueryable

The IQueryable to remove duplicates from.

comparer
IEqualityComparer

An IEqualityComparer to compare values.

Returns

An IQueryable that contains distinct elements from source.

Attributes

Exceptions

source or comparer is null.

Remarks

The Distinct(IQueryable, IEqualityComparer) method generates a MethodCallExpression that represents calling Distinct(IQueryable, IEqualityComparer) itself as a constructed generic method. It then passes the MethodCallExpression to the CreateQuery(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(IQueryable, IEqualityComparer) 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

Distinct(IQueryable)

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

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

public:
generic 
[System::Runtime::CompilerServices::Extension]
 static System::Linq::IQueryable ^ Distinct(System::Linq::IQueryable ^ source);
public static System.Linq.IQueryable Distinct(this System.Linq.IQueryable source);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")]
public static System.Linq.IQueryable Distinct(this System.Linq.IQueryable source);
static member Distinct : System.Linq.IQueryable<'Source> -> System.Linq.IQueryable<'Source>
[]
static member Distinct : System.Linq.IQueryable<'Source> -> System.Linq.IQueryable<'Source>

Public Function Distinct(Of TSource) (source As IQueryable(Of TSource)) As IQueryable(Of TSource)

Type Parameters

TSource

The type of the elements of source.

Parameters

source
IQueryable

The IQueryable to remove duplicates from.

Returns

An IQueryable that contains distinct elements from source.

Attributes

Exceptions

source is null.

Examples

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

List ages = new List { 21, 46, 46, 55, 17, 21, 55, 55 };

IEnumerable 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
*/
Dim ages As List(Of Integer) = New List(Of Integer)(New Integer() {21, 46, 46, 55, 17, 21, 55, 55})

Dim distinctAges As IEnumerable(Of Integer) = ages.AsQueryable().Distinct()

Dim output As New System.Text.StringBuilder
output.AppendLine("Distinct ages:")

For Each age As Integer In distinctAges
    output.AppendLine(age)
Next

' Display the output.
MsgBox(output.ToString())

' This code produces the following output:
'
' Distinct(ages)
' 21
' 46
' 55
' 17

Remarks

The Distinct(IQueryable) method generates a MethodCallExpression that represents calling Distinct(IQueryable) itself as a constructed generic method. It then passes the MethodCallExpression to the CreateQuery(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(IQueryable) 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