Queryable.Union 方法

定義

產生兩個序列的集合等位。Produces the set union of two sequences.

多載

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

使用預設相等比較值來比較值,以便產生兩個序列的集合等位。Produces the set union of two sequences by using the default equality comparer.

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

使用指定的 IEqualityComparer<T> 產生兩個序列的集合等位。Produces the set union of two sequences by using a specified IEqualityComparer<T>.

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

使用預設相等比較值來比較值,以便產生兩個序列的集合等位。Produces the set union of two sequences by using the default equality comparer.

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

序列,其獨特項目構成等位作業的第一個集合。A sequence whose distinct elements form the first set for the union operation.

source2
IEnumerable<TSource>

序列,其獨特項目構成等位作業的第二個集合。A sequence whose distinct elements form the second set for the union operation.

傳回

IQueryable<TSource>

IQueryable<T>,其中包含來自兩個輸入序列的項目,但不包括重複的項目。An IQueryable<T> that contains the elements from both input sequences, excluding duplicates.

例外狀況

source1source2nullsource1 or source2 is null.

範例

下列程式碼範例示範如何使用 Union<TSource>(IQueryable<TSource>, IEnumerable<TSource>) 來取得兩個序列的集合聯集。The following code example demonstrates how to use Union<TSource>(IQueryable<TSource>, IEnumerable<TSource>) to obtain the set union of two sequences.

int[] ints1 = { 5, 3, 9, 7, 5, 9, 3, 7 };
int[] ints2 = { 8, 3, 6, 4, 4, 9, 1, 0 };

// Get the set union of the items in the two arrays.
IEnumerable<int> union = ints1.AsQueryable().Union(ints2);

foreach (int num in union)
    Console.Write("{0} ", num);

/*
    This code produces the following output:

    5 3 9 7 8 6 4 1 0
*/
Dim ints1() As Integer = {5, 3, 9, 7, 5, 9, 3, 7}
Dim ints2() As Integer = {8, 3, 6, 4, 4, 9, 1, 0}

' Get the set union of the items in the two arrays.
Dim union = ints1.AsQueryable().Union(ints2)

Dim output As New System.Text.StringBuilder
For Each num As Integer In union
    output.Append(String.Format("{0} ", num))
Next

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

' This code produces the following output:

' 5 3 9 7 8 6 4 1 0

備註

Union<TSource>(IQueryable<TSource>, IEnumerable<TSource>)方法 MethodCallExpression 會產生,代表呼叫 Union<TSource>(IQueryable<TSource>, IEnumerable<TSource>) 本身做為已建立的泛型方法。The Union<TSource>(IQueryable<TSource>, IEnumerable<TSource>) method generates a MethodCallExpression that represents calling Union<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 the source1 parameter.

執行表示呼叫的運算式樹狀架構所產生的查詢行為, Union<TSource>(IQueryable<TSource>, IEnumerable<TSource>) 取決於參數類型的實作為 source1The query behavior that occurs as a result of executing an expression tree that represents calling Union<TSource>(IQueryable<TSource>, IEnumerable<TSource>) depends on the implementation of the type of the source1 parameter. 預期的行為是,和中的元素的集合聯 source1source2 會傳回。The expected behavior is that the set union of the elements in source1 and source2 is returned.

適用於

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

使用指定的 IEqualityComparer<T> 產生兩個序列的集合等位。Produces the set union of two sequences by using a specified IEqualityComparer<T>.

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

序列,其獨特項目構成等位作業的第一個集合。A sequence whose distinct elements form the first set for the union operation.

source2
IEnumerable<TSource>

序列,其獨特項目構成等位作業的第二個集合。A sequence whose distinct elements form the second set for the union operation.

comparer
IEqualityComparer<TSource>

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

傳回

IQueryable<TSource>

IQueryable<T>,其中包含來自兩個輸入序列的項目,但不包括重複的項目。An IQueryable<T> that contains the elements from both input sequences, excluding duplicates.

例外狀況

source1source2nullsource1 or source2 is null.

備註

Union<TSource>(IQueryable<TSource>, IEnumerable<TSource>, IEqualityComparer<TSource>)方法 MethodCallExpression 會產生,代表呼叫 Union<TSource>(IQueryable<TSource>, IEnumerable<TSource>, IEqualityComparer<TSource>) 本身做為已建立的泛型方法。The Union<TSource>(IQueryable<TSource>, IEnumerable<TSource>, IEqualityComparer<TSource>) method generates a MethodCallExpression that represents calling Union<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 the source1 parameter.

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

適用於