英語で読む

次の方法で共有


Enumerable.Union メソッド

定義

2 つのシーケンスの集合和集合を生成します。

オーバーロード

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

既定の等値比較子を使用して、2 つのシーケンスのセット和集合を生成します。

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

指定した IEqualityComparer<T>を使用して、2 つのシーケンスのセット和集合を生成します。

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

ソース:
Union.cs
ソース:
Union.cs
ソース:
Union.cs

既定の等値比較子を使用して、2 つのシーケンスのセット和集合を生成します。

C#
public static System.Collections.Generic.IEnumerable<TSource> Union<TSource> (this System.Collections.Generic.IEnumerable<TSource> first, System.Collections.Generic.IEnumerable<TSource> second);

型パラメーター

TSource

入力シーケンスの要素の型。

パラメーター

first
IEnumerable<TSource>

共用体の最初のセットを形成する個別の要素を持つ IEnumerable<T>

second
IEnumerable<TSource>

共用体の 2 番目のセットを形成する個別の要素を持つ IEnumerable<T>

戻り値

IEnumerable<TSource>

重複を除く、両方の入力シーケンスの要素を含む IEnumerable<T>

例外

first または secondnull

次のコード例では、Union<TSource>(IEnumerable<TSource>, IEnumerable<TSource>) を使用して 2 つの整数シーケンスの和集合を取得する方法を示します。

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

IEnumerable<int> union = ints1.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
*/

カスタム データ型のオブジェクトのシーケンスを比較する場合は、ヘルパー クラスに IEquatable<T> ジェネリック インターフェイスを実装する必要があります。 次のコード例は、カスタム データ型でこのインターフェイスを実装し、GetHashCode メソッドと Equals メソッドをオーバーライドする方法を示しています。

C#
public class ProductA : IEquatable<ProductA>
{
    public string Name { get; set; }
    public int Code { get; set; }

    public bool Equals(ProductA other)
    {
        if (other is null)
            return false;

        return this.Name == other.Name && this.Code == other.Code;
    }

    public override bool Equals(object obj) => Equals(obj as ProductA);
    public override int GetHashCode() => (Name, Code).GetHashCode();
}

このインターフェイスを実装した後は、次の例に示すように、Union<TSource>(IEnumerable<TSource>, IEnumerable<TSource>) メソッドで ProductA オブジェクトのシーケンスを使用できます。

C#
ProductA[] store1 = { new ProductA { Name = "apple", Code = 9 },
                       new ProductA { Name = "orange", Code = 4 } };

ProductA[] store2 = { new ProductA { Name = "apple", Code = 9 },
                       new ProductA { Name = "lemon", Code = 12 } };
C#
//Get the products from the both arrays
//excluding duplicates.

IEnumerable<ProductA> union =
  store1.Union(store2);

foreach (var product in union)
    Console.WriteLine(product.Name + " " + product.Code);

/*
    This code produces the following output:

    apple 9
    orange 4
    lemon 12
*/

注釈

このメソッドは、遅延実行を使用して実装されます。 即時戻り値は、アクションの実行に必要なすべての情報を格納するオブジェクトです。 このメソッドで表されるクエリは、GetEnumerator メソッドを直接呼び出すか、C# で foreach を使用するか、Visual Basic で For Each を使用してオブジェクトが列挙されるまで実行されません。

このメソッドは、戻り値セットから重複を除外します。 これは、重複を含む入力シーケンス内のすべての要素を返す、Concat メソッドとは異なる動作です。

既定の等値比較子 (Default) は、IEqualityComparer<T> ジェネリック インターフェイスを実装する型の値を比較するために使用されます。 カスタム データ型を比較するには、このインターフェイスを実装し、型に独自の GetHashCode メソッドと Equals メソッドを提供する必要があります。

このメソッドによって返されるオブジェクトを列挙すると、Union はその順序で firstsecond を列挙し、まだ生成されていない各要素を生成します。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

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

ソース:
Union.cs
ソース:
Union.cs
ソース:
Union.cs

指定した IEqualityComparer<T>を使用して、2 つのシーケンスのセット和集合を生成します。

C#
public static System.Collections.Generic.IEnumerable<TSource> Union<TSource> (this System.Collections.Generic.IEnumerable<TSource> first, System.Collections.Generic.IEnumerable<TSource> second, System.Collections.Generic.IEqualityComparer<TSource> comparer);
C#
public static System.Collections.Generic.IEnumerable<TSource> Union<TSource> (this System.Collections.Generic.IEnumerable<TSource> first, System.Collections.Generic.IEnumerable<TSource> second, System.Collections.Generic.IEqualityComparer<TSource>? comparer);

型パラメーター

TSource

入力シーケンスの要素の型。

パラメーター

first
IEnumerable<TSource>

共用体の最初のセットを形成する個別の要素を持つ IEnumerable<T>

second
IEnumerable<TSource>

共用体の 2 番目のセットを形成する個別の要素を持つ IEnumerable<T>

comparer
IEqualityComparer<TSource>

値を比較する IEqualityComparer<T>

戻り値

IEnumerable<TSource>

重複を除く、両方の入力シーケンスの要素を含む IEnumerable<T>

例外

first または secondnull

次の例は、Union メソッドで使用できる等価比較子を実装する方法を示しています。

C#
public class Product
{
    public string Name { get; set; }
    public int Code { get; set; }
}

// Custom comparer for the Product class
class ProductComparer : IEqualityComparer<Product>
{
    // Products are equal if their names and product numbers are equal.
    public bool Equals(Product x, Product y)
    {

        //Check whether the compared objects reference the same data.
        if (Object.ReferenceEquals(x, y)) return true;

        //Check whether any of the compared objects is null.
        if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
            return false;

        //Check whether the products' properties are equal.
        return x.Code == y.Code && x.Name == y.Name;
    }

    // If Equals() returns true for a pair of objects
    // then GetHashCode() must return the same value for these objects.

    public int GetHashCode(Product product)
    {
        //Check whether the object is null
        if (Object.ReferenceEquals(product, null)) return 0;

        //Get hash code for the Name field if it is not null.
        int hashProductName = product.Name == null ? 0 : product.Name.GetHashCode();

        //Get hash code for the Code field.
        int hashProductCode = product.Code.GetHashCode();

        //Calculate the hash code for the product.
        return hashProductName ^ hashProductCode;
    }
}

この比較子を実装した後は、次の例に示すように、Union メソッドで Product オブジェクトのシーケンスを使用できます。

C#
Product[] store10 = { new Product { Name = "apple", Code = 9 },
                       new Product { Name = "orange", Code = 4 } };

Product[] store20 = { new Product { Name = "apple", Code = 9 },
                       new Product { Name = "lemon", Code = 12 } };

//Get the products from the both arrays
//excluding duplicates.

IEnumerable<Product> union =
  store10.Union(store20, new ProductComparer());

foreach (Product product in union)
    Console.WriteLine(product.Name + " " + product.Code);

/*
    This code produces the following output:

    apple 9
    orange 4
    lemon 12
*/

注釈

このメソッドは、遅延実行を使用して実装されます。 即時戻り値は、アクションの実行に必要なすべての情報を格納するオブジェクトです。 このメソッドで表されるクエリは、GetEnumerator メソッドを直接呼び出すか、C# で foreach を使用するか、Visual Basic で For Each を使用してオブジェクトが列挙されるまで実行されません。

comparernullの場合、既定の等値比較子 (Default) を使用して値が比較されます。

このメソッドによって返されるオブジェクトを列挙すると、Union はその順序で firstsecond を列挙し、まだ生成されていない各要素を生成します。

Concat メソッドは、Concat メソッドが重複を含む入力シーケンス内のすべての要素を返すのに対し、Union は一意の値のみを返すので、Union メソッドとは異なります。

こちらもご覧ください

  • UnionBy メソッド を する

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0