Queryable.Distinct Metodo

Definizione

Restituisce elementi distinti da una sequenza.Returns distinct elements from a sequence.

Overload

Distinct<TSource>(IQueryable<TSource>)

Restituisce elementi distinti da una sequenza utilizzando l'operatore di confronto uguaglianze predefinito per confrontare i valori.Returns distinct elements from a sequence by using the default equality comparer to compare values.

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

Restituisce elementi distinti da una sequenza utilizzando uno specificato IEqualityComparer<T> per confrontare valori.Returns distinct elements from a sequence by using a specified IEqualityComparer<T> to compare values.

Distinct<TSource>(IQueryable<TSource>)

Restituisce elementi distinti da una sequenza utilizzando l'operatore di confronto uguaglianze predefinito per confrontare i valori.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)

Parametri di tipo

TSource

Tipo degli elementi di source.The type of the elements of source.

Parametri

source
IQueryable<TSource>

Oggetto IQueryable<T> da cui rimuovere i duplicati.The IQueryable<T> to remove duplicates from.

Restituisce

IQueryable<TSource>

Oggetto IQueryable<T> che contiene elementi distinti da source.An IQueryable<T> that contains distinct elements from source.

Eccezioni

source è null.source is null.

Esempio

Nell'esempio di codice riportato di seguito viene illustrato come utilizzare Distinct<TSource>(IQueryable<TSource>) per restituire elementi distinti da una sequenza.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

Commenti

Il Distinct<TSource>(IQueryable<TSource>) metodo genera un oggetto MethodCallExpression che rappresenta la chiamata a Distinct<TSource>(IQueryable<TSource>) se stesso come metodo generico costruito.The Distinct<TSource>(IQueryable<TSource>) method generates a MethodCallExpression that represents calling Distinct<TSource>(IQueryable<TSource>) itself as a constructed generic method. Passa quindi al MethodCallExpression CreateQuery<TElement>(Expression) metodo dell'oggetto IQueryProvider rappresentato dalla Provider proprietà del source parametro.It then passes the MethodCallExpression to the CreateQuery<TElement>(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

Il comportamento della query che si verifica in seguito all'esecuzione di un albero delle espressioni che rappresenta la chiamata Distinct<TSource>(IQueryable<TSource>) dipende dall'implementazione del tipo di source parametro.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. Il comportamento previsto è che restituisce una sequenza non ordinata di elementi univoci in source .The expected behavior is that it returns an unordered sequence of the unique items in source.

Si applica a

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

Restituisce elementi distinti da una sequenza utilizzando uno specificato IEqualityComparer<T> per confrontare valori.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);
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)

Parametri di tipo

TSource

Tipo degli elementi di source.The type of the elements of source.

Parametri

source
IQueryable<TSource>

Oggetto IQueryable<T> da cui rimuovere i duplicati.The IQueryable<T> to remove duplicates from.

comparer
IEqualityComparer<TSource>

Oggetto IEqualityComparer<T> per confrontare i valori.An IEqualityComparer<T> to compare values.

Restituisce

IQueryable<TSource>

Oggetto IQueryable<T> che contiene elementi distinti da source.An IQueryable<T> that contains distinct elements from source.

Eccezioni

source o comparer è null.source or comparer is null.

Commenti

Il Distinct<TSource>(IQueryable<TSource>, IEqualityComparer<TSource>) metodo genera un oggetto MethodCallExpression che rappresenta la chiamata a Distinct<TSource>(IQueryable<TSource>, IEqualityComparer<TSource>) se stesso come metodo generico costruito.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. Passa quindi al MethodCallExpression CreateQuery<TElement>(Expression) metodo dell'oggetto IQueryProvider rappresentato dalla Provider proprietà del source parametro.It then passes the MethodCallExpression to the CreateQuery<TElement>(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

Il comportamento della query che si verifica in seguito all'esecuzione di un albero delle espressioni che rappresenta la chiamata Distinct<TSource>(IQueryable<TSource>, IEqualityComparer<TSource>) dipende dall'implementazione del tipo di source parametro.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. Il comportamento previsto prevede che restituisca una sequenza non ordinata degli elementi univoci in utilizzando source comparer per confrontare i valori.The expected behavior is that it returns an unordered sequence of the unique items in source by using comparer to compare values.

Si applica a