Enumerable.Concat(IEnumerable<TSource>, IEnumerable<TSource>) Enumerable.Concat(IEnumerable<TSource>, IEnumerable<TSource>) Enumerable.Concat(IEnumerable<TSource>, IEnumerable<TSource>) Enumerable.Concat(IEnumerable<TSource>, IEnumerable<TSource>) Method

정의

두 시퀀스를 연결합니다.Concatenates two sequences.

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

입력 시퀀스 요소의 형식입니다.The type of the elements of the input sequences.

매개 변수

first
IEnumerable<TSource>

연결할 첫 번째 시퀀스입니다.The first sequence to concatenate.

second
IEnumerable<TSource>

첫 번째 시퀀스에 연결할 시퀀스입니다.The sequence to concatenate to the first sequence.

반환

IEnumerable<TSource>

IEnumerable<T> 두 입력된 시퀀스의 연결된 된 요소가 들어 있는입니다.An IEnumerable<T> that contains the concatenated elements of the two input sequences.

예외

first 또는 secondnull인 경우first or second is null.

예제

다음 코드 예제에 사용 하는 방법을 보여 줍니다. Concat<TSource>(IEnumerable<TSource>, IEnumerable<TSource>) 두 시퀀스를 연결 합니다.The following code example demonstrates how to use Concat<TSource>(IEnumerable<TSource>, IEnumerable<TSource>) to concatenate two sequences.

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.
    MsgBox(output.ToString())
End Sub

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

두 시퀀스를 연결 하는 또 다른 방법은 컬렉션, 배열, 예를 들어 시퀀스를 생성 한 다음 적용 하는 것을 SelectMany 메서드를 id 선택기 함수를 전달 합니다.An alternative way of concatenating two sequences is to construct a collection, for example an array, of sequences and then apply the SelectMany method, passing it the identity selector function. 다음 예제에서는이 사용 방법을 보여 줍니다. SelectMany합니다.The following example demonstrates this use of 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.
    MsgBox(output.ToString())

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

설명

이 메서드는 지연 된 실행을 사용 하 여 구현 됩니다.This method is implemented by using deferred execution. 즉시 반환 값은 작업을 수행 하는 데 필요한 모든 정보를 저장 하는 개체입니다.The immediate return value is an object that stores all the information that is required to perform the action. 이 메서드를 나타내는 쿼리 하거나 호출 하 여 개체 열거 될 때까지 실행 되지 않습니다 해당 GetEnumerator 메서드를 사용 하 여 직접 또는 foreach Visual C# 또는 For Each Visual Basic의 합니다.The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or For Each in Visual Basic.

합니다 Concat<TSource>(IEnumerable<TSource>, IEnumerable<TSource>) 에서 다른 메서드를 Union 메서드 때문에 Concat<TSource>(IEnumerable<TSource>, IEnumerable<TSource>) 메서드는 입력된 시퀀스에서 원래 모든 요소를 반환 합니다.The Concat<TSource>(IEnumerable<TSource>, IEnumerable<TSource>) method differs from the Union method because the Concat<TSource>(IEnumerable<TSource>, IEnumerable<TSource>) method returns all the original elements in the input sequences. Union 고유 요소만 반환 합니다.The Union method returns only unique elements.

적용 대상