Queryable.Distinct Method

Definition

Retorna elementos diferentes de uma sequência.Returns distinct elements from a sequence.

Overloads

Distinct<TSource>(IQueryable<TSource>)

Retorna os elementos distintos de uma sequência usando o comparador de igualdade padrão para comparar valores.Returns distinct elements from a sequence by using the default equality comparer to compare values.

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

Retorna os elementos distintos de uma sequência usando um IEqualityComparer<T> especificado para comparar valores.Returns distinct elements from a sequence by using a specified IEqualityComparer<T> to compare values.

Distinct<TSource>(IQueryable<TSource>)

Retorna os elementos distintos de uma sequência usando o comparador de igualdade padrão para comparar valores.Returns distinct elements from a sequence by using the default equality comparer to compare values.

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static System::Linq::IQueryable<TSource> ^ Distinct(System::Linq::IQueryable<TSource> ^ source);
public static System.Linq.IQueryable<TSource> Distinct<TSource> (this System.Linq.IQueryable<TSource> source);
static member Distinct : System.Linq.IQueryable<'Source> -> System.Linq.IQueryable<'Source>
<Extension()>
Public Function Distinct(Of TSource) (source As IQueryable(Of TSource)) As IQueryable(Of TSource)

Type Parameters

TSource

O tipo dos elementos de source.The type of the elements of source.

Parameters

source
IQueryable<TSource>

O IQueryable<T> do qual as duplicatas serão removidas.The IQueryable<T> to remove duplicates from.

Returns

IQueryable<TSource>

Um IQueryable<T> que contém elementos distintos do source.An IQueryable<T> that contains distinct elements from source.

Exceptions

source é null.source is null.

Examples

O exemplo de código a seguir demonstra como usar Distinct<TSource>(IQueryable<TSource>) para retornar elementos distintos de uma sequência.The following code example demonstrates how to use Distinct<TSource>(IQueryable<TSource>) to return distinct elements from a sequence.

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
*/

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

O método Distinct<TSource>(IQueryable<TSource>) gera um MethodCallExpression que representa a chamada Distinct<TSource>(IQueryable<TSource>) si mesmo como um método genérico construído.The Distinct<TSource>(IQueryable<TSource>) method generates a MethodCallExpression that represents calling Distinct<TSource>(IQueryable<TSource>) itself as a constructed generic method. Em seguida, ele passa o MethodCallExpression para o método CreateQuery<TElement>(Expression) do IQueryProvider representado pela propriedade Provider do parâmetro source.It then passes the MethodCallExpression to the CreateQuery<TElement>(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

O comportamento da consulta que ocorre como resultado da execução de uma árvore de expressão que representa a chamada de Distinct<TSource>(IQueryable<TSource>) depende da implementação do tipo do parâmetro source.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. O comportamento esperado é que ele retorna uma sequência não ordenada dos itens exclusivos em source.The expected behavior is that it returns an unordered sequence of the unique items in source.

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

Retorna os elementos distintos de uma sequência usando um IEqualityComparer<T> especificado para comparar valores.Returns distinct elements from a sequence by using a specified IEqualityComparer<T> to compare values.

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static System::Linq::IQueryable<TSource> ^ Distinct(System::Linq::IQueryable<TSource> ^ source, System::Collections::Generic::IEqualityComparer<TSource> ^ comparer);
public static System.Linq.IQueryable<TSource> Distinct<TSource> (this System.Linq.IQueryable<TSource> source, System.Collections.Generic.IEqualityComparer<TSource> comparer);
static member Distinct : System.Linq.IQueryable<'Source> * System.Collections.Generic.IEqualityComparer<'Source> -> System.Linq.IQueryable<'Source>
<Extension()>
Public Function Distinct(Of TSource) (source As IQueryable(Of TSource), comparer As IEqualityComparer(Of TSource)) As IQueryable(Of TSource)

Type Parameters

TSource

O tipo dos elementos de source.The type of the elements of source.

Parameters

source
IQueryable<TSource>

O IQueryable<T> do qual as duplicatas serão removidas.The IQueryable<T> to remove duplicates from.

comparer
IEqualityComparer<TSource>

Um IEqualityComparer<T> para comparar valores.An IEqualityComparer<T> to compare values.

Returns

IQueryable<TSource>

Um IQueryable<T> que contém elementos distintos do source.An IQueryable<T> that contains distinct elements from source.

Exceptions

source ou comparer é null.source or comparer is null.

Remarks

O método Distinct<TSource>(IQueryable<TSource>, IEqualityComparer<TSource>) gera um MethodCallExpression que representa a chamada Distinct<TSource>(IQueryable<TSource>, IEqualityComparer<TSource>) si mesmo como um método genérico construído.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. Em seguida, ele passa o MethodCallExpression para o método CreateQuery<TElement>(Expression) do IQueryProvider representado pela propriedade Provider do parâmetro source.It then passes the MethodCallExpression to the CreateQuery<TElement>(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

O comportamento da consulta que ocorre como resultado da execução de uma árvore de expressão que representa a chamada de Distinct<TSource>(IQueryable<TSource>, IEqualityComparer<TSource>) depende da implementação do tipo do parâmetro source.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. O comportamento esperado é que ele retorna uma sequência não ordenada dos itens exclusivos em source usando comparer para comparar valores.The expected behavior is that it returns an unordered sequence of the unique items in source by using comparer to compare values.

Applies to