Queryable.Except 方法

定義

產生兩個序列的差異。Produces the set difference of two sequences.

多載

Except<TSource>(IQueryable<TSource>, IEnumerable<TSource>, IEqualityComparer<TSource>)

使用指定的 IEqualityComparer<T> 來比較值,以便產生兩個序列的差異。Produces the set difference of two sequences by using the specified IEqualityComparer<T> to compare values.

Except<TSource>(IQueryable<TSource>, IEnumerable<TSource>)

使用預設相等比較子來比較值,以便產生兩個序列的差異。Produces the set difference of two sequences by using the default equality comparer to compare values.

Except<TSource>(IQueryable<TSource>, IEnumerable<TSource>, IEqualityComparer<TSource>)

使用指定的 IEqualityComparer<T> 來比較值,以便產生兩個序列的差異。Produces the set difference of two sequences by using the specified IEqualityComparer<T> to compare values.

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

類型參數

TSource

輸入序列之項目的類型。The type of the elements of the input sequences.

參數

source1
IQueryable<TSource>

IQueryable<T>,其項目若未同時存在 source2 中,便會傳回這些項目。An IQueryable<T> whose elements that are not also in source2 will be returned.

source2
IEnumerable<TSource>

IEnumerable<T>,其項目若同時出現在第一個序列中,則不會出現在傳回的序列中。An IEnumerable<T> whose elements that also occur in the first sequence will not appear in the returned sequence.

comparer
IEqualityComparer<TSource>

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

傳回

IQueryable<TSource>

IQueryable<T>,其中包含兩個序列的差異。An IQueryable<T> that contains the set difference of the two sequences.

例外狀況

source1source2nullsource1 or source2 is null.

備註

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

執行表示呼叫的運算式樹狀架構所產生的查詢行為, Except<TSource>(IQueryable<TSource>, IEnumerable<TSource>, IEqualityComparer<TSource>) 取決於參數類型的實作為 source1The query behavior that occurs as a result of executing an expression tree that represents calling Except<TSource>(IQueryable<TSource>, IEnumerable<TSource>, IEqualityComparer<TSource>) depends on the implementation of the type of the source1 parameter. 預期的行為是傳回中的所有專案,但也會傳回中的所有元素 source1 ,但會 source2 comparer 用來比較值。The expected behavior is that all the elements in source1 are returned except for those that are also in source2, and comparer is used to compare values.

適用於

Except<TSource>(IQueryable<TSource>, IEnumerable<TSource>)

使用預設相等比較子來比較值,以便產生兩個序列的差異。Produces the set difference of two sequences by using the default equality comparer to compare values.

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static System::Linq::IQueryable<TSource> ^ Except(System::Linq::IQueryable<TSource> ^ source1, System::Collections::Generic::IEnumerable<TSource> ^ source2);
public static System.Linq.IQueryable<TSource> Except<TSource> (this System.Linq.IQueryable<TSource> source1, System.Collections.Generic.IEnumerable<TSource> source2);
static member Except : System.Linq.IQueryable<'Source> * seq<'Source> -> System.Linq.IQueryable<'Source>
<Extension()>
Public Function Except(Of TSource) (source1 As IQueryable(Of TSource), source2 As IEnumerable(Of TSource)) As IQueryable(Of TSource)

類型參數

TSource

輸入序列之項目的類型。The type of the elements of the input sequences.

參數

source1
IQueryable<TSource>

IQueryable<T>,其項目若未同時存在 source2 中,便會傳回這些項目。An IQueryable<T> whose elements that are not also in source2 will be returned.

source2
IEnumerable<TSource>

IEnumerable<T>,其項目若同時出現在第一個序列中,則不會出現在傳回的序列中。An IEnumerable<T> whose elements that also occur in the first sequence will not appear in the returned sequence.

傳回

IQueryable<TSource>

IQueryable<T>,其中包含兩個序列的差異。An IQueryable<T> that contains the set difference of the two sequences.

例外狀況

source1source2nullsource1 or source2 is null.

範例

下列程式碼範例示範如何使用傳回 Except<TSource>(IQueryable<TSource>, IEnumerable<TSource>) 只出現在第一個來源序列中的元素。The following code example demonstrates how to use Except<TSource>(IQueryable<TSource>, IEnumerable<TSource>) to return those elements that only appear in the first source sequence.

double[] numbers1 = { 2.0, 2.1, 2.2, 2.3, 2.4, 2.5 };
double[] numbers2 = { 2.2 };

// Get the numbers from the first array that
// are NOT in the second array.
IEnumerable<double> onlyInFirstSet =
    numbers1.AsQueryable().Except(numbers2);

foreach (double number in onlyInFirstSet)
    Console.WriteLine(number);

/*
    This code produces the following output:

    2
    2.1
    2.3
    2.4
    2.5
*/
Dim numbers1() As Double = {2.0, 2.1, 2.2, 2.3, 2.4, 2.5}
Dim numbers2() As Double = {2.2}

' Get the numbers from the first array that
' are NOT in the second array.
Dim onlyInFirstSet As IEnumerable(Of Double) = _
    numbers1.AsQueryable().Except(numbers2)

Dim output As New System.Text.StringBuilder
For Each number As Double In onlyInFirstSet
    output.AppendLine(number)
Next

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

' This code produces the following output:
'
' 2
' 2.1
' 2.3
' 2.4
' 2.5

備註

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

執行表示呼叫的運算式樹狀架構所產生的查詢行為, Except<TSource>(IQueryable<TSource>, IEnumerable<TSource>) 取決於參數類型的實作為 source1The query behavior that occurs as a result of executing an expression tree that represents calling Except<TSource>(IQueryable<TSource>, IEnumerable<TSource>) depends on the implementation of the type of the source1 parameter. 預期的行為是傳回中的所有專案,但也會傳回中的所有元素 source1 source2The expected behavior is that all the elements in source1 are returned except for those that are also in source2.

適用於