Queryable.SelectMany 메서드

정의

시퀀스의 각 요소를 IEnumerable<T>에 투영하고 결과 시퀀스를 IQueryable<T> 형식의 단일 시퀀스로 결합합니다.

오버로드

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

시퀀스의 각 요소를 IEnumerable<T>에 투영하고 각 해당 요소에 대해 결과 선택기 함수를 호출합니다. 각 중간 시퀀스의 결과 값을 1차원 단일 시퀀스로 결합하여 반환합니다.

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

시퀀스의 각 요소를 해당 소스 요소의 인덱스를 통합하는 IEnumerable<T>에 투영합니다. 각 중간 시퀀스의 각 요소에 대해 결과 선택기 함수를 호출하고 결과 값을 1차원 단일 시퀀스로 결합하여 반환합니다.

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

시퀀스의 각 요소를 IEnumerable<T>에 투영하고 결과 시퀀스를 단일 시퀀스로 결합합니다.

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

시퀀스의 각 요소를 IEnumerable<T>에 투영하고 결과 시퀀스를 단일 시퀀스로 결합합니다. 각 소스 요소의 인덱스는 해당 요소의 투영된 폼에 사용됩니다.

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

Source:
Queryable.cs
Source:
Queryable.cs
Source:
Queryable.cs

시퀀스의 각 요소를 IEnumerable<T>에 투영하고 각 해당 요소에 대해 결과 선택기 함수를 호출합니다. 각 중간 시퀀스의 결과 값을 1차원 단일 시퀀스로 결합하여 반환합니다.

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

형식 매개 변수

TSource

source 요소의 형식입니다.

TCollection

collectionSelector가 나타내는 함수가 수집한 중간 요소의 형식입니다.

TResult

결과 시퀀스 요소의 형식입니다.

매개 변수

source
IQueryable<TSource>

계산할 값의 시퀀스입니다.

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

입력 시퀀스의 각 요소에 적용할 프로젝션 함수입니다.

resultSelector
Expression<Func<TSource,TCollection,TResult>>

각 중간 시퀀스의 각 요소에 적용할 프로젝션 함수입니다.

반환

IQueryable<TResult>

해당 요소가 collectionSelector의 각 요소에 대해 일대다 프로젝션 함수 source를 호출한 다음 이러한 시퀀스 요소와 해당 source 요소를 각각 결과 요소에 매핑한 결과인 IQueryable<T>입니다.

예외

source, collectionSelector 또는 resultSelectornull인 경우

예제

다음 코드 예제에서는 를 사용하여 SelectMany<TSource,TCollection,TResult>(IQueryable<TSource>, Expression<Func<TSource, IEnumerable<TCollection>>>, Expression<Func<TSource,TCollection, TResult>>) 배열에 대한 일대다 프로젝션을 수행하는 방법을 보여 줍니다. 이 예제에서는 결과 선택기 함수를 사용하여 에 대한 최종 호출Select에 대해 scope 각 중간 시퀀스에 해당하는 원본 요소를 유지합니다.

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

class Pet
{
    public string Name { get; set; }
    public string Breed { get; set; }
}

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

    // This query demonstrates how to obtain a sequence of
    // the names of all the pets whose breed is "Collie", while
    // keeping an association with the owner that owns the pet.
    var query =
        petOwners.AsQueryable()
        // Create a sequence of ALL the Pet objects. Then
        // project an anonymous type that consists of each
        // Pet in the new sequence and the PetOwner object
        // from the initial array that corresponds to that pet.
       .SelectMany(owner => owner.Pets,
                   (owner, pet) => new { owner, pet })
        // Filter the sequence of anonymous types to only
        // keep pets whose breed is "Collie".
        .Where(ownerAndPet => ownerAndPet.pet.Breed == "Collie")
        // Project an anonymous type that consists
        // of the pet owner's name and the pet's name.
        .Select(ownerAndPet => new
        {
            Owner = ownerAndPet.owner.Name,
            Pet = ownerAndPet.pet.Name
        });

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

/* This code produces the following output:

    { Owner = Ashkenazi, Pet = Walker }
    { Owner = Price, Pet = Diesel }
    { Owner = Hines, Pet = Dusty }
*/
Structure PetOwner
    Public Name As String
    Public Pets As List(Of Pet)
End Structure

Structure Pet
    Public Name As String
    Public Breed As String
End Structure

Shared Sub SelectManyEx3()
    Dim petOwners() As PetOwner = _
                {New PetOwner With {.Name = "Higa", _
                      .Pets = New List(Of Pet)(New Pet() { _
                          New Pet With {.Name = "Scruffy", .Breed = "Poodle"}, _
                          New Pet With {.Name = "Sam", .Breed = "Hound"}})}, _
                  New PetOwner With {.Name = "Ashkenazi", _
                      .Pets = New List(Of Pet)(New Pet() { _
                          New Pet With {.Name = "Walker", .Breed = "Collie"}, _
                          New Pet With {.Name = "Sugar", .Breed = "Poodle"}})}, _
                  New PetOwner With {.Name = "Price", _
                      .Pets = New List(Of Pet)(New Pet() { _
                          New Pet With {.Name = "Scratches", .Breed = "Dachshund"}, _
                          New Pet With {.Name = "Diesel", .Breed = "Collie"}})}, _
                  New PetOwner With {.Name = "Hines", _
                      .Pets = New List(Of Pet)(New Pet() { _
                          New Pet With {.Name = "Dusty", .Breed = "Collie"}})} _
                }

    ' This query demonstrates how to obtain a sequence of
    ' the names of all the pets whose breed is "Collie", while
    ' keeping an association with the owner that owns the pet.
    Dim query = petOwners.AsQueryable() _
        .SelectMany(Function(owner) owner.Pets, _
               Function(owner, pet) New With {owner, pet}) _
        .Where(Function(ownerAndPet) ownerAndPet.pet.Breed = "Collie") _
        .Select(Function(ownerAndPet) New With { _
            .Owner = ownerAndPet.owner.Name, _
            .Pet = ownerAndPet.pet.Name})

    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=Ashkenazi, Pet=Walker
' Owner=Price, Pet=Diesel
' Owner=Hines, Pet=Dusty

설명

이 메서드에는 형식 인수가 형식 중 하나인 형식 Expression<TDelegate> 의 매개 변수가 Func<T,TResult> 하나 이상 있습니다. 이러한 매개 변수의 경우 람다 식을 전달할 수 있으며 에 컴파일됩니다 Expression<TDelegate>.

메서드는 SelectMany<TSource,TCollection,TResult>(IQueryable<TSource>, Expression<Func<TSource, IEnumerable<TCollection>>>, Expression<Func<TSource,TCollection, TResult>>)MethodCallExpression 생성된 제네릭 메서드로 자신을 호출 SelectMany<TSource,TCollection,TResult>(IQueryable<TSource>, Expression<Func<TSource, IEnumerable<TCollection>>>, Expression<Func<TSource,TCollection, TResult>>) 하는 를 나타내는 을 생성합니다. 그런 다음 을 MethodCallExpression 매개 변수의 CreateQuery(Expression) 속성으로 나타내는 의 IQueryProvider 메서드에 Providersource 전달합니다.

호출 SelectMany<TSource,TCollection,TResult>(IQueryable<TSource>, Expression<Func<TSource, IEnumerable<TCollection>>>, Expression<Func<TSource,TCollection, TResult>>) 을 나타내는 식 트리를 실행한 결과로 발생하는 쿼리 동작은 매개 변수 형식의 source 구현에 따라 달라집니다. 예상되는 동작은 의 source 각 요소에서 를 호출 collectionSelector 하여 열거 가능한 형식으로 프로젝션하는 것입니다. 그런 다음 에서 resultSelector 나타내는 함수는 각 중간 시퀀스의 각 요소에서 호출됩니다. 결과 값은 단일 1차원 시퀀스로 연결됩니다.

적용 대상

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

Source:
Queryable.cs
Source:
Queryable.cs
Source:
Queryable.cs

시퀀스의 각 요소를 해당 소스 요소의 인덱스를 통합하는 IEnumerable<T>에 투영합니다. 각 중간 시퀀스의 각 요소에 대해 결과 선택기 함수를 호출하고 결과 값을 1차원 단일 시퀀스로 결합하여 반환합니다.

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

형식 매개 변수

TSource

source 요소의 형식입니다.

TCollection

collectionSelector가 나타내는 함수가 수집한 중간 요소의 형식입니다.

TResult

결과 시퀀스 요소의 형식입니다.

매개 변수

source
IQueryable<TSource>

계산할 값의 시퀀스입니다.

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

입력 시퀀스의 각 요소에 적용할 프로젝션 함수이며, 이 함수의 두 번째 매개 변수는 소스 요소의 인덱스를 나타냅니다.

resultSelector
Expression<Func<TSource,TCollection,TResult>>

각 중간 시퀀스의 각 요소에 적용할 프로젝션 함수입니다.

반환

IQueryable<TResult>

해당 요소가 collectionSelector의 각 요소에 대해 일대다 프로젝션 함수 source를 호출한 다음 이러한 시퀀스 요소와 해당 source 요소를 각각 결과 요소에 매핑한 결과인 IQueryable<T>입니다.

예외

source, collectionSelector 또는 resultSelectornull인 경우

설명

이 메서드에는 형식 인수가 형식 중 하나인 형식 Expression<TDelegate> 의 매개 변수가 Func<T,TResult> 하나 이상 있습니다. 이러한 매개 변수의 경우 람다 식을 전달할 수 있으며 에 컴파일됩니다 Expression<TDelegate>.

메서드는 SelectMany<TSource,TCollection,TResult>(IQueryable<TSource>, Expression<Func<TSource, Int32,IEnumerable<TCollection>>>, Expression<Func<TSource,TCollection, TResult>>)MethodCallExpression 생성된 제네릭 메서드로 자신을 호출 SelectMany<TSource,TCollection,TResult>(IQueryable<TSource>, Expression<Func<TSource, Int32,IEnumerable<TCollection>>>, Expression<Func<TSource,TCollection, TResult>>) 하는 를 나타내는 을 생성합니다. 그런 다음 을 MethodCallExpression 매개 변수의 CreateQuery(Expression) 속성으로 나타내는 의 IQueryProvider 메서드에 Providersource 전달합니다.

호출 SelectMany<TSource,TCollection,TResult>(IQueryable<TSource>, Expression<Func<TSource, Int32,IEnumerable<TCollection>>>, Expression<Func<TSource,TCollection, TResult>>) 을 나타내는 식 트리를 실행한 결과로 발생하는 쿼리 동작은 매개 변수 형식의 source 구현에 따라 달라집니다. 예상되는 동작은 의 source 각 요소에서 를 호출 collectionSelector 하여 열거 가능한 형식으로 프로젝션하는 것입니다. 각 열거 가능한 결과는 원본 요소의 인덱스를 통합합니다. 그런 다음 에서 resultSelector 나타내는 함수는 각 중간 시퀀스의 각 요소에서 호출됩니다. 결과 값은 단일 1차원 시퀀스로 연결됩니다.

적용 대상

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

Source:
Queryable.cs
Source:
Queryable.cs
Source:
Queryable.cs

시퀀스의 각 요소를 IEnumerable<T>에 투영하고 결과 시퀀스를 단일 시퀀스로 결합합니다.

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

형식 매개 변수

TSource

source 요소의 형식입니다.

TResult

selector가 나타내는 함수에서 반환되는 시퀀스 요소의 형식입니다.

매개 변수

source
IQueryable<TSource>

계산할 값의 시퀀스입니다.

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

각 요소에 적용할 프로젝션 함수입니다.

반환

IQueryable<TResult>

해당 요소가 입력 시퀀스의 각 요소에 대해 일대다 프로젝션 함수를 호출한 결과인 IQueryable<T>입니다.

예외

source 또는 selectornull인 경우

예제

다음 코드 예제에서는 를 사용하여 SelectMany<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,IEnumerable<TResult>>>) 배열에 대한 일대다 프로젝션을 수행하는 방법을 보여 줍니다.

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.AsQueryable().SelectMany(petOwner => petOwner.Pets);

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

    // Only one foreach loop is required to iterate through the
    // results because 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.AsQueryable().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

Shared Sub SelectManyEx1()
    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"}}}

    ' Query using SelectMany().
    Dim query1 As IEnumerable(Of String) = _
                petOwners.AsQueryable().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 shows how to use Select() instead of SelectMany().
    Dim query2 As IEnumerable(Of String()) = _
                petOwners.AsQueryable().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

설명

이 메서드에는 형식 인수가 형식 중 하나인 형식 Expression<TDelegate> 의 매개 변수가 Func<T,TResult> 하나 이상 있습니다. 이러한 매개 변수의 경우 람다 식을 전달할 수 있으며 에 컴파일됩니다 Expression<TDelegate>.

메서드는 SelectMany<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,IEnumerable<TResult>>>)MethodCallExpression 생성된 제네릭 메서드로 자신을 호출 SelectMany<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,IEnumerable<TResult>>>) 하는 를 나타내는 을 생성합니다. 그런 다음 을 MethodCallExpression 매개 변수의 CreateQuery(Expression) 속성으로 나타내는 의 IQueryProvider 메서드에 Providersource 전달합니다.

호출 SelectMany<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,IEnumerable<TResult>>>) 을 나타내는 식 트리를 실행한 결과로 발생하는 쿼리 동작은 매개 변수 형식의 source 구현에 따라 달라집니다. 예상되는 동작은 의 source 각 요소에서 를 호출 selector 하여 열거 가능한 형식으로 프로젝션하는 것입니다. 그런 다음 열거 가능한 결과를 단일 1차원 시퀀스로 연결합니다.

적용 대상

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

Source:
Queryable.cs
Source:
Queryable.cs
Source:
Queryable.cs

시퀀스의 각 요소를 IEnumerable<T>에 투영하고 결과 시퀀스를 단일 시퀀스로 결합합니다. 각 소스 요소의 인덱스는 해당 요소의 투영된 폼에 사용됩니다.

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

형식 매개 변수

TSource

source 요소의 형식입니다.

TResult

selector가 나타내는 함수에서 반환되는 시퀀스 요소의 형식입니다.

매개 변수

source
IQueryable<TSource>

계산할 값의 시퀀스입니다.

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

각 요소에 적용할 프로젝션 함수이며, 이 함수의 두 번째 매개 변수는 소스 요소의 인덱스를 나타냅니다.

반환

IQueryable<TResult>

해당 요소가 입력 시퀀스의 각 요소에 대해 일대다 프로젝션 함수를 호출한 결과인 IQueryable<T>입니다.

예외

source 또는 selectornull인 경우

예제

다음 코드 예제에서는 를 사용하여 SelectMany<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,Int32,IEnumerable<TResult>>>) 배열에 대한 일대다 프로젝션을 수행하고 각 원본 요소의 인덱스를 사용하는 방법을 보여 줍니다.

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" } } };

    // For each PetOwner element in the source array,
    // project a sequence of strings where each string
    // consists of the index of the PetOwner element in the
    // source array and the name of each pet in PetOwner.Pets.
    IEnumerable<string> query =
        petOwners.AsQueryable()
        .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

Shared Sub SelectManyEx2()
    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"}}}

    ' For each PetOwner element in the source array,
    ' project a sequence of strings where each string
    ' consists of the index of the PetOwner element in the
    ' source array and the name of each pet in PetOwner.Pets.
    Dim query As IEnumerable(Of String) = _
        petOwners.AsQueryable() _
        .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 code produces the following output:
'
' 0Scruffy
' 0Sam
' 1Walker
' 1Sugar
' 2Scratches
' 2Diesel
' 3Dusty

설명

이 메서드에는 형식 인수가 형식 중 하나인 형식 Expression<TDelegate> 의 매개 변수가 Func<T,TResult> 하나 이상 있습니다. 이러한 매개 변수의 경우 람다 식을 전달할 수 있으며 에 컴파일됩니다 Expression<TDelegate>.

메서드는 SelectMany<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,Int32,IEnumerable<TResult>>>)MethodCallExpression 생성된 제네릭 메서드로 자신을 호출 SelectMany<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,Int32,IEnumerable<TResult>>>) 하는 를 나타내는 을 생성합니다. 그런 다음 을 MethodCallExpression 매개 변수의 CreateQuery(Expression) 속성으로 나타내는 의 IQueryProvider 메서드에 Providersource 전달합니다.

호출 SelectMany<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,Int32,IEnumerable<TResult>>>) 을 나타내는 식 트리를 실행한 결과로 발생하는 쿼리 동작은 매개 변수 형식의 source 구현에 따라 달라집니다. 예상되는 동작은 의 source 각 요소에서 를 호출 selector 하여 열거 가능한 형식으로 프로젝션하는 것입니다. 각 열거 가능한 결과는 원본 요소의 인덱스를 통합합니다. 그런 다음 열거 가능한 결과를 단일 1차원 시퀀스로 연결합니다.

적용 대상