# Queryable.Union 方法

## 重载

 Union(IQueryable, IEnumerable) 通过使用默认的相等比较器，生成两个序列的并集。 Union(IQueryable, IEnumerable, IEqualityComparer) 通过使用指定的 IEqualityComparer 生成两个序列的并集。

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

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

#### 参数

source1
IQueryable<TSource>

source2
IEnumerable<TSource>

#### 返回

IQueryable<TSource>

#### 例外

`source1``source2``null`

### 示例

``````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>, IEqualityComparer<TSource>)

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

#### 参数

source1
IQueryable<TSource>

source2
IEnumerable<TSource>

comparer
IEqualityComparer<TSource>

#### 返回

IQueryable<TSource>

#### 例外

`source1``source2``null`