Enumerable.Concat<TSource> メソッド

定義

2 つのシーケンスを連結します。

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::IEnumerable<TSource> ^ Concat(System::Collections::Generic::IEnumerable<TSource> ^ first, System::Collections::Generic::IEnumerable<TSource> ^ second);
public static System.Collections.Generic.IEnumerable<TSource> Concat<TSource> (this System.Collections.Generic.IEnumerable<TSource> first, System.Collections.Generic.IEnumerable<TSource> second);
static member Concat : seq<'Source> * seq<'Source> -> seq<'Source>
<Extension()>
Public Function Concat(Of TSource) (first As IEnumerable(Of TSource), second As IEnumerable(Of TSource)) As IEnumerable(Of TSource)

型パラメーター

TSource

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

パラメーター

first
IEnumerable<TSource>

連結する最初のシーケンス。

second
IEnumerable<TSource>

最初のシーケンスに連結するシーケンス。

戻り値

IEnumerable<TSource>

2 つの入力シーケンスの連結された要素が格納されている IEnumerable<T>

例外

first または secondnull です。

次のコード例では、 を使用 Concat<TSource>(IEnumerable<TSource>, IEnumerable<TSource>) して 2 つのシーケンスを連結する方法を示します。

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
Structure Pet
    Public Name As String
    Public Age As Integer
End Structure

' Returns an array of Pet objects.
Function GetCats() As Pet()
    Dim cats() As Pet = {New Pet With {.Name = "Barley", .Age = 8},
                 New Pet With {.Name = "Boots", .Age = 4},
                 New Pet With {.Name = "Whiskers", .Age = 1}}

    Return cats
End Function

' Returns an array of Pet objects.
Function GetDogs() As Pet()
    Dim dogs() As Pet = {New Pet With {.Name = "Bounder", .Age = 3},
                 New Pet With {.Name = "Snoopy", .Age = 14},
                 New Pet With {.Name = "Fido", .Age = 9}}
    Return dogs
End Function

Sub ConcatEx1()
    ' Create two arrays of Pet objects.
    Dim cats() As Pet = GetCats()
    Dim dogs() As Pet = GetDogs()

    ' Project the Name of each cat and concatenate
    ' the collection of cat name strings with a collection
    ' of dog name strings.
    Dim query As IEnumerable(Of String) =
cats _
.Select(Function(cat) cat.Name) _
.Concat(dogs.Select(Function(dog) dog.Name))

    Dim output As New System.Text.StringBuilder
    For Each name As String In query
        output.AppendLine(name)
    Next

    ' Display the output.
    Console.WriteLine(output.ToString())
End Sub

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

2 つのシーケンスを連結する別の方法は、配列などのシーケンスのコレクションを構築し、 メソッドを SelectMany 適用して ID セレクター関数を渡すことです。 次の例では、 のこの使用方法を SelectMany示します。

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
    ' Create two arrays of Pet objects.
    Dim cats() As Pet = GetCats()
    Dim dogs() As Pet = GetDogs()

    ' Create an IEnumerable collection that contains two elements.
    ' Each element is an array of Pet objects.
    Dim animals() As IEnumerable(Of Pet) = {cats, dogs}

    Dim query As IEnumerable(Of String) =
(animals.SelectMany(Function(pets) _
                        pets.Select(Function(pet) pet.Name)))

    Dim output As New System.Text.StringBuilder
    For Each name As String In query
        output.AppendLine(name)
    Next

    ' Display the output.
    Console.WriteLine(output.ToString())

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

注釈

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

メソッドはConcat<TSource>(IEnumerable<TSource>, IEnumerable<TSource>)、入力シーケンス内のすべての元のConcat<TSource>(IEnumerable<TSource>, IEnumerable<TSource>)要素を返すので、 メソッドと異なりますUnion。 メソッドは Union 一意の要素のみを返します。

適用対象