Queryable.Distinct Queryable.Distinct Queryable.Distinct Queryable.Distinct Method

定義

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

多載

Distinct<TSource>(IQueryable<TSource>) Distinct<TSource>(IQueryable<TSource>) Distinct<TSource>(IQueryable<TSource>) 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>) Distinct<TSource>(IQueryable<TSource>, IEqualityComparer<TSource>) Distinct<TSource>(IQueryable<TSource>, IEqualityComparer<TSource>) 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>) Distinct<TSource>(IQueryable<TSource>) Distinct<TSource>(IQueryable<TSource>) 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.

例外狀況

範例

下列程式碼範例示範如何使用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)至由Provider source參數IQueryProvider的屬性所表示之的方法。It 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>)之運算式樹狀架構而產生的查詢行為, 取決於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. 預期的行為是它會在中source傳回唯一專案的未排序序列。The expected behavior is that it returns an unordered sequence of the unique items in source.

Distinct<TSource>(IQueryable<TSource>, IEqualityComparer<TSource>) Distinct<TSource>(IQueryable<TSource>, IEqualityComparer<TSource>) Distinct<TSource>(IQueryable<TSource>, IEqualityComparer<TSource>) 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);
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)至由Provider source參數IQueryProvider的屬性所表示之的方法。It 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>)之運算式樹狀架構而產生的查詢行為, 取決於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. 預期的行為是, 它會使用source comparer來比較值, 以傳回中唯一專案的未排序序列。The expected behavior is that it returns an unordered sequence of the unique items in source by using comparer to compare values.

適用於