Enumerable.Concat<TSource> 메서드

정의

두 시퀀스를 연결합니다.

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>

두 입력 시퀀스의 연결된 요소가 들어 있는 IEnumerable<T>입니다.

예외

first 또는 secondnull인 경우

예제

다음 코드 예제에서는 두 시퀀스를 연결 하는 데 사용 Concat<TSource>(IEnumerable<TSource>, IEnumerable<TSource>) 하는 방법을 보여 줍니다.

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

두 시퀀스를 연결하는 다른 방법은 컬렉션(예: 시퀀스의 배열)을 생성한 다음 메서드를 적용 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>) 메서드가 Union 입력 시퀀스의 모든 원래 요소를 반환하기 때문에 Concat<TSource>(IEnumerable<TSource>, IEnumerable<TSource>) 메서드와 다릅니다. 메서드는 Union 고유 요소만 반환합니다.

적용 대상