Enumerable.Union Yöntem

Tanım

İki dizinin küme birleşimini üretir.

Aşırı Yüklemeler

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

Varsayılan eşitlik karşılaştırıcısını kullanarak iki sıranın küme birleşimini üretir.

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

Belirtilen IEqualityComparer<T>bir kullanarak iki sıranın küme birleşimini üretir.

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

Kaynak:
Union.cs
Kaynak:
Union.cs
Kaynak:
Union.cs

Varsayılan eşitlik karşılaştırıcısını kullanarak iki sıranın küme birleşimini üretir.

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

Tür Parametreleri

TSource

Giriş dizilerinin öğelerinin türü.

Parametreler

first
IEnumerable<TSource>

Birleşim IEnumerable<T> için ilk kümeyi oluşturan benzersiz öğeleri.

second
IEnumerable<TSource>

Benzersiz IEnumerable<T> öğeleri birleşim için ikinci kümeyi oluşturan bir.

Döndürülenler

IEnumerable<TSource>

IEnumerable<T> Yinelenenler hariç her iki giriş dizisinden öğeleri içeren bir.

Özel durumlar

first veya second şeklindedir null.

Örnekler

Aşağıdaki kod örneği, iki tamsayı dizisinin birleşimini elde etmek için nasıl kullanılacağını Union<TSource>(IEnumerable<TSource>, IEnumerable<TSource>) gösterir.

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
*/

Bazı özel veri türlerindeki nesne dizilerini karşılaştırmak istiyorsanız, yardımcı sınıfta genel arabirimi uygulamanız IEquatable<T> gerekir. Aşağıdaki kod örneği, bu arabirimin özel bir veri türünde nasıl uygulanıp geçersiz kılınıp ve Equals yöntemlerin nasıl uygulanıp uygulanmayacaklarını GetHashCode gösterir.

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();
}

Bu arabirimi uyguladıktan sonra, aşağıdaki örnekte gösterildiği gibi yöntemindeki Union<TSource>(IEnumerable<TSource>, IEnumerable<TSource>) nesne dizilerini ProductA kullanabilirsiniz:

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
*/

Açıklamalar

Bu yöntem ertelenmiş yürütme kullanılarak uygulanır. Hemen dönüş değeri, eylemi gerçekleştirmek için gereken tüm bilgileri depolayan bir nesnedir. Bu yöntemle temsil edilen sorgu, doğrudan yöntemini çağırarak GetEnumerator veya C# içinde veya For Each Visual Basic'te kullanarak foreach nesne numaralandırılana kadar yürütülür.

Bu yöntem, yinelenenleri dönüş kümesinden dışlar. Bu, yinelenenler de dahil olmak üzere giriş dizilerindeki tüm öğeleri döndüren yönteminden farklı bir davranıştır Concat .

Varsayılan eşitlik karşılaştırıcısı, Defaultgenel arabirimi uygulayan IEqualityComparer<T> türlerin değerlerini karşılaştırmak için kullanılır. Özel bir veri türünü karşılaştırmak için bu arabirimi uygulamanız ve türü için kendi GetHashCode ve Equals yöntemlerinizi sağlamanız gerekir.

Bu yöntem tarafından döndürülen nesne numaralandırıldığında, Union ve second sırasıyla numaralandırılır first ve henüz verilmemiş olan her öğeyi verir.

Şunlara uygulanır

.NET 9 ve diğer sürümler
Ürün Sürümler
.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>)

Kaynak:
Union.cs
Kaynak:
Union.cs
Kaynak:
Union.cs

Belirtilen IEqualityComparer<T>bir kullanarak iki sıranın küme birleşimini üretir.

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);

Tür Parametreleri

TSource

Giriş dizilerinin öğelerinin türü.

Parametreler

first
IEnumerable<TSource>

Birleşim IEnumerable<T> için ilk kümeyi oluşturan benzersiz öğeleri.

second
IEnumerable<TSource>

Benzersiz IEnumerable<T> öğeleri birleşim için ikinci kümeyi oluşturan bir.

comparer
IEqualityComparer<TSource>

IEqualityComparer<T> Değerleri karşılaştırmak için.

Döndürülenler

IEnumerable<TSource>

IEnumerable<T> Yinelenenler hariç her iki giriş dizisinden öğeleri içeren bir.

Özel durumlar

first veya second şeklindedir null.

Örnekler

Aşağıdaki örnekte yönteminde kullanılabilecek bir eşitlik karşılaştırıcısının nasıl uygulanacağı gösterilmektedir 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;
    }
}

Bu karşılaştırıcıyı uyguladıktan sonra, aşağıdaki örnekte gösterildiği gibi yöntemindeki Union nesne dizilerini Product kullanabilirsiniz:

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
*/

Açıklamalar

Bu yöntem ertelenmiş yürütme kullanılarak uygulanır. Hemen dönüş değeri, eylemi gerçekleştirmek için gereken tüm bilgileri depolayan bir nesnedir. Bu yöntemle temsil edilen sorgu, doğrudan yöntemini çağırarak GetEnumerator veya C# içinde veya For Each Visual Basic'te kullanarak foreach nesne numaralandırılana kadar yürütülür.

ise comparernull, değerleri karşılaştırmak için varsayılan eşitlik karşılaştırıcısı Defaultkullanılır.

Bu yöntem tarafından döndürülen nesne numaralandırıldığında, Union ve second sırasıyla numaralandırılır first ve henüz verilmemiş olan her öğeyi verir.

Concat yöntemi, yinelenenler de dahil olmak üzere giriş dizilerindeki tüm öğeleri döndürdüğünden yöntemi Concat yönteminden Union farklıdır, ancak Union yalnızca benzersiz değerler döndürür.

Şunlara uygulanır

.NET 9 ve diğer sürümler
Ürün Sürümler
.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