Queryable.Distinct 方法

定義

從序列傳回獨特的項目。Returns distinct elements from a sequence.

多載

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>)

使用指定的 IEqualityComparer<T> 來比較值,以便從序列傳回獨特的項目。Returns distinct elements from a sequence by using a specified IEqualityComparer<T> to compare values.

Distinct<TSource>(IQueryable<TSource>)

使用預設的相等比較子來比較值,以便從序列傳回獨特的項目。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)

類型參數

TSource

source 項目的類型。The type of the elements of source.

參數

source
IQueryable<TSource>

要從中移除重複項目的 IQueryable<T>The IQueryable<T> to remove duplicates from.

傳回

IQueryable<TSource>

IQueryable<T>,其中包含來自 source 的獨特項目。An IQueryable<T> that contains distinct elements from source.

例外狀況

sourcenullsource is null.

範例

下列程式碼範例示範如何使用 Distinct<TSource>(IQueryable<TSource>) 從序列傳回相異元素。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

備註

Distinct<TSource>(IQueryable<TSource>)方法 MethodCallExpression 會產生,代表呼叫 Distinct<TSource>(IQueryable<TSource>) 本身做為已建立的泛型方法。The Distinct<TSource>(IQueryable<TSource>) method generates a MethodCallExpression that represents calling Distinct<TSource>(IQueryable<TSource>) itself as a constructed generic method. 然後,它會將傳遞 MethodCallExpression 給以 CreateQuery<TElement>(Expression) 參數的屬性所表示的方法 IQueryProvider Provider sourceIt then passes the MethodCallExpression to the CreateQuery<TElement>(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

執行表示呼叫的運算式樹狀架構所產生的查詢行為, Distinct<TSource>(IQueryable<TSource>) 取決於參數類型的實作為 sourceThe 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. 預期的行為是它會在中傳回唯一專案的未排序次序 sourceThe expected behavior is that it returns an unordered sequence of the unique items in source.

適用於

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

使用指定的 IEqualityComparer<T> 來比較值,以便從序列傳回獨特的項目。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)

類型參數

TSource

source 項目的類型。The type of the elements of source.

參數

source
IQueryable<TSource>

要從中移除重複項目的 IQueryable<T>The IQueryable<T> to remove duplicates from.

comparer
IEqualityComparer<TSource>

用來比較值的 IEqualityComparer<T>An IEqualityComparer<T> to compare values.

傳回

IQueryable<TSource>

IQueryable<T>,其中包含來自 source 的獨特項目。An IQueryable<T> that contains distinct elements from source.

例外狀況

sourcecomparernullsource or comparer is null.

備註

Distinct<TSource>(IQueryable<TSource>, IEqualityComparer<TSource>)方法 MethodCallExpression 會產生,代表呼叫 Distinct<TSource>(IQueryable<TSource>, IEqualityComparer<TSource>) 本身做為已建立的泛型方法。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. 然後,它會將傳遞 MethodCallExpression 給以 CreateQuery<TElement>(Expression) 參數的屬性所表示的方法 IQueryProvider Provider sourceIt then passes the MethodCallExpression to the CreateQuery<TElement>(Expression) method of the IQueryProvider represented by the Provider property of the source parameter.

執行表示呼叫的運算式樹狀架構所產生的查詢行為, Distinct<TSource>(IQueryable<TSource>, IEqualityComparer<TSource>) 取決於參數類型的實作為 sourceThe 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. 預期的行為是 source 使用來比較值,以傳回中唯一專案的未排序序列 comparerThe expected behavior is that it returns an unordered sequence of the unique items in source by using comparer to compare values.

適用於