Enumerable.SelectMany Enumerable.SelectMany Enumerable.SelectMany Enumerable.SelectMany Method

정의

시퀀스의 각 요소는 IEnumerable<T> 고 결과 시퀀스를 단일 시퀀스로 평면화 합니다.Projects each element of a sequence to an IEnumerable<T> and flattens the resulting sequences into one sequence.

오버로드

SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>) SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>) SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>) SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>)

시퀀스의 각 요소는 IEnumerable<T>, 결과 시퀀스를 단일 시퀀스로 평면화 및 포함 된 각 요소에 대해 결과 선택기 함수를 호출 합니다.Projects each element of a sequence to an IEnumerable<T>, flattens the resulting sequences into one sequence, and invokes a result selector function on each element therein.

SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>) SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>) SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>) SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>)

시퀀스의 각 요소는 IEnumerable<T>, 결과 시퀀스를 단일 시퀀스로 평면화 및 포함 된 각 요소에 대해 결과 선택기 함수를 호출 합니다.Projects each element of a sequence to an IEnumerable<T>, flattens the resulting sequences into one sequence, and invokes a result selector function on each element therein. 각 소스 요소의 인덱스는 해당 요소의 투영된 중간 폼에 사용됩니다.The index of each source element is used in the intermediate projected form of that element.

SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>) SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>) SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>) SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>)

시퀀스의 각 요소는 IEnumerable<T> 고 결과 시퀀스를 단일 시퀀스로 평면화 합니다.Projects each element of a sequence to an IEnumerable<T> and flattens the resulting sequences into one sequence.

SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>) SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>) SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>) SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>)

시퀀스의 각 요소는 IEnumerable<T>, 고 결과 시퀀스를 단일 시퀀스로 평면화 합니다.Projects each element of a sequence to an IEnumerable<T>, and flattens the resulting sequences into one sequence. 각 소스 요소의 인덱스는 해당 요소의 투영된 폼에 사용됩니다.The index of each source element is used in the projected form of that element.

SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>) SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>) SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>) SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>)

시퀀스의 각 요소는 IEnumerable<T>, 결과 시퀀스를 단일 시퀀스로 평면화 및 포함 된 각 요소에 대해 결과 선택기 함수를 호출 합니다.Projects each element of a sequence to an IEnumerable<T>, flattens the resulting sequences into one sequence, and invokes a result selector function on each element therein.

public:
generic <typename TSource, typename TCollection, typename TResult>
[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::IEnumerable<TResult> ^ SelectMany(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, System::Collections::Generic::IEnumerable<TCollection> ^> ^ collectionSelector, Func<TSource, TCollection, TResult> ^ resultSelector);
public static System.Collections.Generic.IEnumerable<TResult> SelectMany<TSource,TCollection,TResult> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,System.Collections.Generic.IEnumerable<TCollection>> collectionSelector, Func<TSource,TCollection,TResult> resultSelector);
static member SelectMany : seq<'Source> * Func<'Source, seq<'Collection>> * Func<'Source, 'Collection, 'Result> -> seq<'Result>
<Extension()>
Public Function SelectMany(Of TSource, TCollection, TResult) (source As IEnumerable(Of TSource), collectionSelector As Func(Of TSource, IEnumerable(Of TCollection)), resultSelector As Func(Of TSource, TCollection, TResult)) As IEnumerable(Of TResult)

형식 매개 변수

TSource

source 요소의 형식입니다.The type of the elements of source.

TCollection

수집 하는 중간 요소의 형식 collectionSelector합니다.The type of the intermediate elements collected by collectionSelector.

TResult

결과 시퀀스 요소의 형식입니다.The type of the elements of the resulting sequence.

매개 변수

source
IEnumerable<TSource>

계산할 값의 시퀀스입니다.A sequence of values to project.

collectionSelector
Func<TSource,IEnumerable<TCollection>>

입력된 시퀀스의 각 요소에 적용할 변형 함수입니다.A transform function to apply to each element of the input sequence.

resultSelector
Func<TSource,TCollection,TResult>

중간 시퀀스의 각 요소에 적용할 변형 함수입니다.A transform function to apply to each element of the intermediate sequence.

반환

IEnumerable<TResult>

IEnumerable<T> 요소가-일대다 변환 함수를 호출한 결과인 collectionSelector 의 각 요소에 대해 source 및 다음 이러한 시퀀스 요소와 해당 소스 요소를 각각 결과 요소에 매핑.An IEnumerable<T> whose elements are the result of invoking the one-to-many transform function collectionSelector on each element of source and then mapping each of those sequence elements and their corresponding source element to a result element.

예외

source, collectionSelector 또는 resultSelectornull인 경우source or collectionSelector or resultSelector is null.

예제

다음 코드 예제에 사용 하는 방법을 보여 줍니다 SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>) 배열에 일 대 다 프로젝션을 수행 하 고 결과 선택기 함수에 대 한 마지막 호출에 대 한 범위에는 소스 시퀀스의 각 해당 요소를 유지 하는 데에 Select입니다.The following code example demonstrates how to use SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>) to perform a one-to-many projection over an array and use a result selector function to keep each corresponding element from the source sequence in scope for the final call to Select.

class PetOwner
{
    public string Name { get; set; }
    public List<string> Pets { get; set; }
}

public static void SelectManyEx3()
{
    PetOwner[] petOwners =
        { new PetOwner { Name="Higa", 
              Pets = new List<string>{ "Scruffy", "Sam" } },
          new PetOwner { Name="Ashkenazi", 
              Pets = new List<string>{ "Walker", "Sugar" } },
          new PetOwner { Name="Price", 
              Pets = new List<string>{ "Scratches", "Diesel" } },
          new PetOwner { Name="Hines", 
              Pets = new List<string>{ "Dusty" } } };

    // Project the pet owner's name and the pet's name.
    var query =
        petOwners
        .SelectMany(petOwner => petOwner.Pets, (petOwner, petName) => new { petOwner, petName })
        .Where(ownerAndPet => ownerAndPet.petName.StartsWith("S"))
        .Select(ownerAndPet =>
                new
                {
                    Owner = ownerAndPet.petOwner.Name,
                    Pet = ownerAndPet.petName
                }
        );

    // Print the results.
    foreach (var obj in query)
    {
        Console.WriteLine(obj);
    }
}

// This code produces the following output:
//
// {Owner=Higa, Pet=Scruffy}
// {Owner=Higa, Pet=Sam}
// {Owner=Ashkenazi, Pet=Sugar}
// {Owner=Price, Pet=Scratches}
Structure PetOwner
    Public Name As String
    Public Pets() As String
End Structure

Sub SelectManyEx3()
    ' Create an array of PetOwner objects.
    Dim petOwners() As PetOwner =
{New PetOwner With
 {.Name = "Higa", .Pets = New String() {"Scruffy", "Sam"}},
 New PetOwner With
 {.Name = "Ashkenazi", .Pets = New String() {"Walker", "Sugar"}},
 New PetOwner With
 {.Name = "Price", .Pets = New String() {"Scratches", "Diesel"}},
 New PetOwner With
 {.Name = "Hines", .Pets = New String() {"Dusty"}}}

    ' Project an anonymous type that consists of
    ' the owner's name and the pet's name (string).
    Dim query =
petOwners _
.SelectMany(
    Function(petOwner) petOwner.Pets,
    Function(petOwner, petName) New With {petOwner, petName}) _
.Where(Function(ownerAndPet) ownerAndPet.petName.StartsWith("S")) _
.Select(Function(ownerAndPet) _
       New With {.Owner = ownerAndPet.petOwner.Name,
                 .Pet = ownerAndPet.petName
       })

    Dim output As New System.Text.StringBuilder
    For Each obj In query
        output.AppendLine(String.Format("Owner={0}, Pet={1}", obj.Owner, obj.Pet))
    Next

    ' Display the output.
    MsgBox(output.ToString())
End Sub

' This code produces the following output:
'
' Owner=Higa, Pet=Scruffy
' Owner=Higa, Pet=Sam
' Owner=Ashkenazi, Pet=Sugar
' Owner=Price, Pet=Scratches

설명

이 메서드는 지연 된 실행을 사용 하 여 구현 됩니다.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.

합니다 SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>) 의 요소를 유지 해야 할 경우 메서드는 유용 source 범위에 대 한 호출 뒤에 오는 쿼리 논리에 SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>)입니다.The SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>) method is useful when you have to keep the elements of source in scope for query logic that occurs after the call to SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>). 코드 예제는 예제 부분을 참조하십시오.See the Example section for a code example. 형식의 개체 간의 양방향 관계 인지 TSource 형식의 개체 TCollection, 즉 형식의 개체인 경우 TCollection 검색 하는 속성을 제공 합니다 TSource 를 생성 한 개체를이 필요 하지 않습니다 오버 로드 SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>)합니다.If there is a bidirectional relationship between objects of type TSource and objects of type TCollection, that is, if an object of type TCollection provides a property to retrieve the TSource object that produced it, you do not need this overload of SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>). 대신 사용할 수 있습니다 SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>) 로 다시 이동 합니다 TSource 개체를 통해를 TCollection 개체입니다.Instead, you can use SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>) and navigate back to the TSource object through the TCollection object.

쿼리 식 구문에서 각 from 절 (Visual C#) 또는 From 의 호출으로 변환 하나는 초기 뒤에 절 (Visual Basic) SelectMany합니다.In query expression syntax, each from clause (Visual C#) or From clause (Visual Basic) after the initial one translates to an invocation of SelectMany.

추가 정보

SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>) SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>) SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>) SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>)

시퀀스의 각 요소는 IEnumerable<T>, 결과 시퀀스를 단일 시퀀스로 평면화 및 포함 된 각 요소에 대해 결과 선택기 함수를 호출 합니다.Projects each element of a sequence to an IEnumerable<T>, flattens the resulting sequences into one sequence, and invokes a result selector function on each element therein. 각 소스 요소의 인덱스는 해당 요소의 투영된 중간 폼에 사용됩니다.The index of each source element is used in the intermediate projected form of that element.

public:
generic <typename TSource, typename TCollection, typename TResult>
[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::IEnumerable<TResult> ^ SelectMany(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, int, System::Collections::Generic::IEnumerable<TCollection> ^> ^ collectionSelector, Func<TSource, TCollection, TResult> ^ resultSelector);
public static System.Collections.Generic.IEnumerable<TResult> SelectMany<TSource,TCollection,TResult> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,int,System.Collections.Generic.IEnumerable<TCollection>> collectionSelector, Func<TSource,TCollection,TResult> resultSelector);
static member SelectMany : seq<'Source> * Func<'Source, int, seq<'Collection>> * Func<'Source, 'Collection, 'Result> -> seq<'Result>
<Extension()>
Public Function SelectMany(Of TSource, TCollection, TResult) (source As IEnumerable(Of TSource), collectionSelector As Func(Of TSource, Integer, IEnumerable(Of TCollection)), resultSelector As Func(Of TSource, TCollection, TResult)) As IEnumerable(Of TResult)

형식 매개 변수

TSource

source 요소의 형식입니다.The type of the elements of source.

TCollection

수집 하는 중간 요소의 형식 collectionSelector합니다.The type of the intermediate elements collected by collectionSelector.

TResult

결과 시퀀스 요소의 형식입니다.The type of the elements of the resulting sequence.

매개 변수

source
IEnumerable<TSource>

계산할 값의 시퀀스입니다.A sequence of values to project.

collectionSelector
Func<TSource,Int32,IEnumerable<TCollection>>

각 소스 요소에 적용할 변형 함수이며, 이 함수의 두 번째 매개 변수는 소스 요소의 인덱스를 나타냅니다.A transform function to apply to each source element; the second parameter of the function represents the index of the source element.

resultSelector
Func<TSource,TCollection,TResult>

중간 시퀀스의 각 요소에 적용할 변형 함수입니다.A transform function to apply to each element of the intermediate sequence.

반환

IEnumerable<TResult>

IEnumerable<T> 요소가-일대다 변환 함수를 호출한 결과인 collectionSelector 의 각 요소에 대해 source 및 다음 이러한 시퀀스 요소와 해당 소스 요소를 각각 결과 요소에 매핑.An IEnumerable<T> whose elements are the result of invoking the one-to-many transform function collectionSelector on each element of source and then mapping each of those sequence elements and their corresponding source element to a result element.

예외

source, collectionSelector 또는 resultSelectornull인 경우source or collectionSelector or resultSelector is null.

설명

이 메서드는 지연 된 실행을 사용 하 여 구현 됩니다.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.

합니다 SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>) 의 요소를 유지 해야 할 경우 메서드는 유용 source 범위에 대 한 호출 뒤에 오는 쿼리 논리에 SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>)입니다.The SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>) method is useful when you have to keep the elements of source in scope for query logic that occurs after the call to SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>). 코드 예제는 예제 부분을 참조하십시오.See the Example section for a code example. 형식의 개체 간의 양방향 관계 인지 TSource 형식의 개체 TCollection, 즉 형식의 개체인 경우 TCollection 검색 하는 속성을 제공 합니다 TSource 를 생성 한 개체를이 필요 하지 않습니다 오버 로드 SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>)합니다.If there is a bidirectional relationship between objects of type TSource and objects of type TCollection, that is, if an object of type TCollection provides a property to retrieve the TSource object that produced it, you do not need this overload of SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>). 대신 사용할 수 있습니다 SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>) 로 다시 이동 합니다 TSource 개체를 통해를 TCollection 개체입니다.Instead, you can use SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>) and navigate back to the TSource object through the TCollection object.

SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>) SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>) SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>) SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>)

시퀀스의 각 요소는 IEnumerable<T> 고 결과 시퀀스를 단일 시퀀스로 평면화 합니다.Projects each element of a sequence to an IEnumerable<T> and flattens the resulting sequences into one sequence.

public:
generic <typename TSource, typename TResult>
[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::IEnumerable<TResult> ^ SelectMany(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, System::Collections::Generic::IEnumerable<TResult> ^> ^ selector);
public static System.Collections.Generic.IEnumerable<TResult> SelectMany<TSource,TResult> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,System.Collections.Generic.IEnumerable<TResult>> selector);
static member SelectMany : seq<'Source> * Func<'Source, seq<'Result>> -> seq<'Result>
<Extension()>
Public Function SelectMany(Of TSource, TResult) (source As IEnumerable(Of TSource), selector As Func(Of TSource, IEnumerable(Of TResult))) As IEnumerable(Of TResult)

형식 매개 변수

TSource

source 요소의 형식입니다.The type of the elements of source.

TResult

반환 된 시퀀스의 요소 형식의 selector합니다.The type of the elements of the sequence returned by selector.

매개 변수

source
IEnumerable<TSource>

계산할 값의 시퀀스입니다.A sequence of values to project.

selector
Func<TSource,IEnumerable<TResult>>

각 요소에 적용할 변형 함수입니다.A transform function to apply to each element.

반환

IEnumerable<TResult>

IEnumerable<T> 요소가 입력된 시퀀스의 각 요소에 대해-일대다 변환 함수를 호출 하면 됩니다.An IEnumerable<T> whose elements are the result of invoking the one-to-many transform function on each element of the input sequence.

예외

source 또는 selectornull인 경우source or selector is null.

예제

다음 코드 예제에 사용 하는 방법을 보여 줍니다. SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>) -일대다 프로젝션 배열에 대해 수행 하 합니다.The following code example demonstrates how to use SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>) to perform a one-to-many projection over an array.

class PetOwner
{
    public string Name { get; set; }
    public List<String> Pets { get; set; }
}

public static void SelectManyEx1()
{
    PetOwner[] petOwners = 
        { new PetOwner { Name="Higa, Sidney", 
              Pets = new List<string>{ "Scruffy", "Sam" } },
          new PetOwner { Name="Ashkenazi, Ronen", 
              Pets = new List<string>{ "Walker", "Sugar" } },
          new PetOwner { Name="Price, Vernette", 
              Pets = new List<string>{ "Scratches", "Diesel" } } };

    // Query using SelectMany().
    IEnumerable<string> query1 = petOwners.SelectMany(petOwner => petOwner.Pets);

    Console.WriteLine("Using SelectMany():");

    // Only one foreach loop is required to iterate 
    // through the results since it is a
    // one-dimensional collection.
    foreach (string pet in query1)
    {
        Console.WriteLine(pet);
    }

    // This code shows how to use Select() 
    // instead of SelectMany().
    IEnumerable<List<String>> query2 =
        petOwners.Select(petOwner => petOwner.Pets);

    Console.WriteLine("\nUsing Select():");

    // Notice that two foreach loops are required to 
    // iterate through the results
    // because the query returns a collection of arrays.
    foreach (List<String> petList in query2)
    {
        foreach (string pet in petList)
        {
            Console.WriteLine(pet);
        }
        Console.WriteLine();
    }
}

/*
 This code produces the following output:

 Using SelectMany():
 Scruffy
 Sam
 Walker
 Sugar
 Scratches
 Diesel

 Using Select():
 Scruffy
 Sam

 Walker
 Sugar

 Scratches
 Diesel
*/
Structure PetOwner
    Public Name As String
    Public Pets() As String
End Structure

Sub SelectManyEx1()
    ' Create an array of PetOwner objects.
    Dim petOwners() As PetOwner =
{New PetOwner With
 {.Name = "Higa, Sidney", .Pets = New String() {"Scruffy", "Sam"}},
 New PetOwner With
 {.Name = "Ashkenazi, Ronen", .Pets = New String() {"Walker", "Sugar"}},
 New PetOwner With
 {.Name = "Price, Vernette", .Pets = New String() {"Scratches", "Diesel"}}}

    ' Call SelectMany() to gather all pets into a "flat" sequence.
    Dim query1 As IEnumerable(Of String) =
petOwners.SelectMany(Function(petOwner) petOwner.Pets)

    Dim output As New System.Text.StringBuilder("Using SelectMany():" & vbCrLf)
    ' Only one foreach loop is required to iterate through 
    ' the results because it is a one-dimensional collection.
    For Each pet As String In query1
        output.AppendLine(pet)
    Next

    ' This code demonstrates how to use Select() instead 
    ' of SelectMany() to get the same result.
    Dim query2 As IEnumerable(Of String()) =
petOwners.Select(Function(petOwner) petOwner.Pets)
    output.AppendLine(vbCrLf & "Using Select():")
    ' Notice that two foreach loops are required to iterate through 
    ' the results because the query returns a collection of arrays.
    For Each petArray() As String In query2
        For Each pet As String In petArray
            output.AppendLine(pet)
        Next
    Next

    ' Display the output.
    MsgBox(output.ToString())
End Sub

' This code produces the following output:
'
' Using SelectMany():
' Scruffy
' Sam
' Walker
' Sugar
' Scratches
' Diesel
'
' Using Select():
' Scruffy
' Sam
' Walker
' Sugar
' Scratches
' Diesel

설명

이 메서드는 지연 된 실행을 사용 하 여 구현 됩니다.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.

SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>) 메서드는 입력된 시퀀스를 열거, 변형 함수를 사용 하 여 각 요소에 IEnumerable<T>, 다음 열거 하 고 이러한 각의 요소를 생성 IEnumerable<T> 개체입니다.The SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>) method enumerates the input sequence, uses a transform function to map each element to an IEnumerable<T>, and then enumerates and yields the elements of each such IEnumerable<T> object. 즉,의 각 요소에 대해 source, selector 가 호출 값 시퀀스를 반환 됩니다.That is, for each element of source, selector is invoked and a sequence of values is returned. SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>) 그런 다음이 2 차원 컬렉션을 1 차원으로 컬렉션을 평면화 IEnumerable<T> 반환 합니다.SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>) then flattens this two-dimensional collection of collections into a one-dimensional IEnumerable<T> and returns it. 예를 들어, 쿼리를 사용 하는 경우 SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>) 주문을 가져오려면 (형식의 Order) 형식의 결과 데이터베이스에 각 고객에 대해 IEnumerable<Order> C# 또는 IEnumerable(Of Order) Visual Basic의 합니다.For example, if a query uses SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>) to obtain the orders (of type Order) for each customer in a database, the result is of type IEnumerable<Order> in C# or IEnumerable(Of Order) in Visual Basic. 대신 쿼리를 사용 하는 경우 Select 주문을 가져오는에 주문 컬렉션의 컬렉션 결합 되지 않으며 결과 형식입니다 IEnumerable<List<Order>> C# 또는 IEnumerable(Of List(Of Order)) Visual Basic의 합니다.If instead the query uses Select to obtain the orders, the collection of collections of orders is not combined and the result is of type IEnumerable<List<Order>> in C# or IEnumerable(Of List(Of Order)) in Visual Basic.

쿼리 식 구문에서 각 from 절 (Visual C#) 또는 From 의 호출으로 변환 하나는 초기 뒤에 절 (Visual Basic) SelectMany합니다.In query expression syntax, each from clause (Visual C#) or From clause (Visual Basic) after the initial one translates to an invocation of SelectMany.

추가 정보

SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>) SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>) SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>) SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>)

시퀀스의 각 요소는 IEnumerable<T>, 고 결과 시퀀스를 단일 시퀀스로 평면화 합니다.Projects each element of a sequence to an IEnumerable<T>, and flattens the resulting sequences into one sequence. 각 소스 요소의 인덱스는 해당 요소의 투영된 폼에 사용됩니다.The index of each source element is used in the projected form of that element.

public:
generic <typename TSource, typename TResult>
[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::IEnumerable<TResult> ^ SelectMany(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, int, System::Collections::Generic::IEnumerable<TResult> ^> ^ selector);
public static System.Collections.Generic.IEnumerable<TResult> SelectMany<TSource,TResult> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,int,System.Collections.Generic.IEnumerable<TResult>> selector);
static member SelectMany : seq<'Source> * Func<'Source, int, seq<'Result>> -> seq<'Result>
<Extension()>
Public Function SelectMany(Of TSource, TResult) (source As IEnumerable(Of TSource), selector As Func(Of TSource, Integer, IEnumerable(Of TResult))) As IEnumerable(Of TResult)

형식 매개 변수

TSource

source 요소의 형식입니다.The type of the elements of source.

TResult

반환 된 시퀀스의 요소 형식의 selector합니다.The type of the elements of the sequence returned by selector.

매개 변수

source
IEnumerable<TSource>

계산할 값의 시퀀스입니다.A sequence of values to project.

selector
Func<TSource,Int32,IEnumerable<TResult>>

각 소스 요소에 적용할 변형 함수이며, 이 함수의 두 번째 매개 변수는 소스 요소의 인덱스를 나타냅니다.A transform function to apply to each source element; the second parameter of the function represents the index of the source element.

반환

IEnumerable<TResult>

IEnumerable<T> 요소가 입력된 시퀀스의 각 요소에 대해-일대다 변환 함수 호출의 결과입니다.An IEnumerable<T> whose elements are the result of invoking the one-to-many transform function on each element of an input sequence.

예외

source 또는 selectornull인 경우source or selector is null.

예제

다음 코드 예제에 사용 하는 방법을 보여 줍니다. SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>) -일대다 프로젝션 배열에 대해 수행 하 고 각 외부 요소의 인덱스를 사용 하 합니다.The following code example demonstrates how to use SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>) to perform a one-to-many projection over an array and use the index of each outer element.

class PetOwner
{
    public string Name { get; set; }
    public List<string> Pets { get; set; }
}

public static void SelectManyEx2()
{
    PetOwner[] petOwners = 
        { new PetOwner { Name="Higa, Sidney", 
              Pets = new List<string>{ "Scruffy", "Sam" } },
          new PetOwner { Name="Ashkenazi, Ronen", 
              Pets = new List<string>{ "Walker", "Sugar" } },
          new PetOwner { Name="Price, Vernette", 
              Pets = new List<string>{ "Scratches", "Diesel" } },
          new PetOwner { Name="Hines, Patrick", 
              Pets = new List<string>{ "Dusty" } } };

    // Project the items in the array by appending the index 
    // of each PetOwner to each pet's name in that petOwner's 
    // array of pets.
    IEnumerable<string> query =
        petOwners.SelectMany((petOwner, index) =>
                                 petOwner.Pets.Select(pet => index + pet));

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

// This code produces the following output:
//
// 0Scruffy
// 0Sam
// 1Walker
// 1Sugar
// 2Scratches
// 2Diesel
// 3Dusty
Structure PetOwner
    Public Name As String
    Public Pets() As String
End Structure

Sub SelectManyEx2()
    ' Create an array of PetOwner objects.
    Dim petOwners() As PetOwner =
{New PetOwner With
 {.Name = "Higa, Sidney", .Pets = New String() {"Scruffy", "Sam"}},
 New PetOwner With
 {.Name = "Ashkenazi, Ronen", .Pets = New String() {"Walker", "Sugar"}},
 New PetOwner With
 {.Name = "Price, Vernette", .Pets = New String() {"Scratches", "Diesel"}},
 New PetOwner With
 {.Name = "Hines, Patrick", .Pets = New String() {"Dusty"}}}

    ' Project the items in the array by appending the index 
    ' of each PetOwner to each pet's name in that petOwner's 
    ' array of pets.
    Dim query As IEnumerable(Of String) =
petOwners.SelectMany(Function(petOwner, index) _
                         petOwner.Pets.Select(Function(pet) _
                                                  index.ToString() + pet))

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

    ' Display the output.
    MsgBox(output.ToString())
End Sub

설명

이 메서드는 지연 된 실행을 사용 하 여 구현 됩니다.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.

SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>) 메서드는 입력된 시퀀스를 열거, 변형 함수를 사용 하 여 각 요소에 IEnumerable<T>, 다음 열거 하 고 이러한 각의 요소를 생성 IEnumerable<T> 개체입니다.The SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>) method enumerates the input sequence, uses a transform function to map each element to an IEnumerable<T>, and then enumerates and yields the elements of each such IEnumerable<T> object. 즉,의 각 요소에 대해 source, selector 가 호출 값 시퀀스를 반환 됩니다.That is, for each element of source, selector is invoked and a sequence of values is returned. SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>) 그런 다음이 2 차원 컬렉션을 1 차원으로 컬렉션을 평면화 IEnumerable<T> 반환 합니다.SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>) then flattens this two-dimensional collection of collections into a one-dimensional IEnumerable<T> and returns it. 예를 들어, 쿼리를 사용 하는 경우 SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>) 주문을 가져오려면 (형식의 Order) 형식의 결과 데이터베이스에 각 고객에 대해 IEnumerable<Order> C# 또는 IEnumerable(Of Order) Visual Basic의 합니다.For example, if a query uses SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>) to obtain the orders (of type Order) for each customer in a database, the result is of type IEnumerable<Order> in C# or IEnumerable(Of Order) in Visual Basic. 대신 쿼리를 사용 하는 경우 Select 주문을 가져오는에 주문 컬렉션의 컬렉션 결합 되지 않으며 결과 형식입니다 IEnumerable<List<Order>> C# 또는 IEnumerable(Of List(Of Order)) Visual Basic의 합니다.If instead the query uses Select to obtain the orders, the collection of collections of orders is not combined and the result is of type IEnumerable<List<Order>> in C# or IEnumerable(Of List(Of Order)) in Visual Basic.

첫 번째 인수 selector 처리 요소를 나타냅니다.The first argument to selector represents the element to process. 두 번째 인수를 selector 소스 시퀀스에서 해당 요소의 0부터 시작 인덱스를 나타냅니다.The second argument to selector represents the zero-based index of that element in the source sequence. 이 요소는 알려진된 순서 대로 및 예를 들어 특정 인덱스에 요소를 사용 하 여 작업을 수행 하려는 경우 유용할 수 있습니다.This can be useful if the elements are in a known order and you want to do something with an element at a particular index, for example. 것도 유용할 수 있습니다 하나 이상의 요소의 인덱스를 검색 하려는 경우.It can also be useful if you want to retrieve the index of one or more elements.

적용 대상