Enumerable.Concat<TSource> Yöntem

Tanım

İki diziyi birleştirir.

C#
public static System.Collections.Generic.IEnumerable<TSource> Concat<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ştirilmeye ilk sıra.

second
IEnumerable<TSource>

İlk sıraya birleştirilme sırası.

Döndürülenler

IEnumerable<TSource>

IEnumerable<T> İki giriş dizisinin birleştirilmiş öğelerini içeren bir.

Özel durumlar

first veya second şeklindedir null.

Örnekler

Aşağıdaki kod örneği, iki diziyi birleştirmek için nasıl kullanılacağını Concat<TSource>(IEnumerable<TSource>, IEnumerable<TSource>) gösterir.

C#
class Pet
{
    public string Name { get; set; }
    public int Age { get; set; }
}

static Pet[] GetCats()
{
    Pet[] cats = { new Pet { Name="Barley", Age=8 },
                   new Pet { Name="Boots", Age=4 },
                   new Pet { Name="Whiskers", Age=1 } };
    return cats;
}

static Pet[] GetDogs()
{
    Pet[] dogs = { new Pet { Name="Bounder", Age=3 },
                   new Pet { Name="Snoopy", Age=14 },
                   new Pet { Name="Fido", Age=9 } };
    return dogs;
}

public static void ConcatEx1()
{
    Pet[] cats = GetCats();
    Pet[] dogs = GetDogs();

    IEnumerable<string> query =
        cats.Select(cat => cat.Name).Concat(dogs.Select(dog => dog.Name));

    foreach (string name in query)
    {
        Console.WriteLine(name);
    }
}

// This code produces the following output:
//
// Barley
// Boots
// Whiskers
// Bounder
// Snoopy
// Fido

İki diziyi birleştirmenin alternatif bir yolu, dizi dizisi gibi bir koleksiyon oluşturmak ve ardından yöntemini uygulayarak SelectMany kimlik seçici işlevini geçirmektir. Aşağıdaki örnekte bu kullanımı gösterilmektedir SelectMany.

C#
Pet[] cats = GetCats();
Pet[] dogs = GetDogs();

IEnumerable<string> query =
    new[] { cats.Select(cat => cat.Name), dogs.Select(dog => dog.Name) }
    .SelectMany(name => name);

foreach (string name in query)
{
    Console.WriteLine(name);
}

// This code produces the following output:
//
// Barley
// Boots
// Whiskers
// Bounder
// Snoopy
// Fido

Açıklamalar

Bu yöntem ertelenmiş yürütme kullanılarak uygulanır. Anında 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.

Concat<TSource>(IEnumerable<TSource>, IEnumerable<TSource>) yöntemi, giriş dizilerindeki tüm özgün öğeleri döndürdüğünden Concat<TSource>(IEnumerable<TSource>, IEnumerable<TSource>) yöntemi yönteminden Union farklıdır. Union yöntemi yalnızca benzersiz öğeleri döndürür.

Şunlara uygulanır

Ü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