Queryable.SelectMany Metoda

Definicja

Projektuje każdy element sekwencji do IEnumerable<T> elementu i łączy wynikowe sekwencje w jedną sekwencję typu IQueryable<T>.

Przeciążenia

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

Projektuje każdy element sekwencji do elementu IEnumerable<T> i wywołuje funkcję selektora wyników dla każdego elementu. Wynikowe wartości z każdej sekwencji pośredniej są łączone w jedną, jednowymiarową sekwencję i zwracane.

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

Projektuje każdy element sekwencji do IEnumerable<T> elementu, który zawiera indeks elementu źródłowego, który go wyprodukował. Funkcja selektora wyników jest wywoływana dla każdego elementu każdej sekwencji pośredniej, a wynikowe wartości są łączone w jedną, jednowymiarową sekwencję i zwracane.

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

Projektuje każdy element sekwencji do elementu IEnumerable<T> i łączy wynikowe sekwencje w jedną sekwencję.

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

Projektuje każdy element sekwencji do elementu IEnumerable<T> i łączy wynikowe sekwencje w jedną sekwencję. Indeks każdego elementu źródłowego jest używany w przewidywanej formie tego elementu.

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

Źródło:
Queryable.cs
Źródło:
Queryable.cs
Źródło:
Queryable.cs

Projektuje każdy element sekwencji do elementu IEnumerable<T> i wywołuje funkcję selektora wyników dla każdego elementu. Wynikowe wartości z każdej sekwencji pośredniej są łączone w jedną, jednowymiarową sekwencję i zwracane.

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)

Parametry typu

TSource

Typ elementów elementu source.

TCollection

Typ elementów pośrednich zebranych przez funkcję reprezentowaną przez collectionSelectorfunkcję .

TResult

Typ elementów wynikowej sekwencji.

Parametry

source
IQueryable<TSource>

Sekwencja wartości do projektu.

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

Funkcja projekcji, która ma być stosowana do każdego elementu sekwencji danych wejściowych.

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

Funkcja projekcji, która ma być stosowana do każdego elementu każdej sekwencji pośredniej.

Zwraca

IQueryable<TResult>

Element IQueryable<T> , którego elementy są wynikiem wywołania funkcji collectionSelector projekcji jeden do wielu w każdym elemecie, source a następnie mapowania każdego z tych elementów sekwencji i odpowiadającego im source elementu wynikowego.

Wyjątki

source lub collectionSelectorresultSelector jest null.

Przykłady

W poniższym przykładzie kodu pokazano, jak wykonać SelectMany<TSource,TCollection,TResult>(IQueryable<TSource>, Expression<Func<TSource, IEnumerable<TCollection>>>, Expression<Func<TSource,TCollection, TResult>>) projekcję jeden do wielu na tablicy. W tym przykładzie użyto funkcji selektora wyników, aby zachować element źródłowy odpowiadający każdej sekwencji pośredniej w zakresie końcowego wywołania do Select.

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

Uwagi

Ta metoda ma co najmniej jeden parametr typu Expression<TDelegate> , którego argument type jest jednym z Func<T,TResult> typów. W przypadku tych parametrów można przekazać wyrażenie lambda i zostanie skompilowane do elementu Expression<TDelegate>.

Metoda SelectMany<TSource,TCollection,TResult>(IQueryable<TSource>, Expression<Func<TSource, IEnumerable<TCollection>>>, Expression<Func<TSource,TCollection, TResult>>) generuje element MethodCallExpression , który reprezentuje wywołanie SelectMany<TSource,TCollection,TResult>(IQueryable<TSource>, Expression<Func<TSource, IEnumerable<TCollection>>>, Expression<Func<TSource,TCollection, TResult>>) siebie jako skonstruowaną metodę ogólną. Następnie przekazuje MethodCallExpression element do CreateQuery(Expression) metody reprezentowanej IQueryProvider przez Provider właściwość parametru source .

Zachowanie zapytania, które występuje w wyniku wykonania drzewa wyrażeń, które reprezentuje wywołanie SelectMany<TSource,TCollection,TResult>(IQueryable<TSource>, Expression<Func<TSource, IEnumerable<TCollection>>>, Expression<Func<TSource,TCollection, TResult>>) , zależy od implementacji typu parametru source . Oczekiwane zachowanie polega na tym, że wywołuje collectionSelector on każdy element source , aby projektować go w formie wyliczalnej. Następnie funkcja reprezentowana przez resultSelector jest wywoływana dla każdego elementu w każdej sekwencji pośredniej. Wynikowe wartości są łączone w jedną, jednowymiarową sekwencję.

Dotyczy

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

Źródło:
Queryable.cs
Źródło:
Queryable.cs
Źródło:
Queryable.cs

Projektuje każdy element sekwencji do IEnumerable<T> elementu, który zawiera indeks elementu źródłowego, który go wyprodukował. Funkcja selektora wyników jest wywoływana dla każdego elementu każdej sekwencji pośredniej, a wynikowe wartości są łączone w jedną, jednowymiarową sekwencję i zwracane.

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)

Parametry typu

TSource

Typ elementów elementu source.

TCollection

Typ elementów pośrednich zebranych przez funkcję reprezentowaną przez collectionSelectorfunkcję .

TResult

Typ elementów wynikowej sekwencji.

Parametry

source
IQueryable<TSource>

Sekwencja wartości do projektu.

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

Funkcja projekcji, która ma być stosowana do każdego elementu sekwencji danych wejściowych; drugi parametr tej funkcji reprezentuje indeks elementu źródłowego.

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

Funkcja projekcji, która ma być stosowana do każdego elementu każdej sekwencji pośredniej.

Zwraca

IQueryable<TResult>

Element IQueryable<T> , którego elementy są wynikiem wywołania funkcji collectionSelector projekcji jeden do wielu w każdym elemecie, source a następnie mapowania każdego z tych elementów sekwencji i odpowiadającego im source elementu wynikowego.

Wyjątki

source lub collectionSelectorresultSelector jest null.

Uwagi

Ta metoda ma co najmniej jeden parametr typu Expression<TDelegate> , którego argument type jest jednym z Func<T,TResult> typów. W przypadku tych parametrów można przekazać wyrażenie lambda i zostanie skompilowane do elementu Expression<TDelegate>.

Metoda SelectMany<TSource,TCollection,TResult>(IQueryable<TSource>, Expression<Func<TSource, Int32,IEnumerable<TCollection>>>, Expression<Func<TSource,TCollection, TResult>>) generuje element MethodCallExpression , który reprezentuje wywołanie SelectMany<TSource,TCollection,TResult>(IQueryable<TSource>, Expression<Func<TSource, Int32,IEnumerable<TCollection>>>, Expression<Func<TSource,TCollection, TResult>>) siebie jako skonstruowaną metodę ogólną. Następnie przekazuje MethodCallExpression element do CreateQuery(Expression) metody reprezentowanej IQueryProvider przez Provider właściwość parametru source .

Zachowanie zapytania, które występuje w wyniku wykonania drzewa wyrażeń, które reprezentuje wywołanie SelectMany<TSource,TCollection,TResult>(IQueryable<TSource>, Expression<Func<TSource, Int32,IEnumerable<TCollection>>>, Expression<Func<TSource,TCollection, TResult>>) , zależy od implementacji typu parametru source . Oczekiwane zachowanie polega na tym, że wywołuje collectionSelector on każdy element source , aby projektować go w formie wyliczalnej. Każdy wynik wyliczalny zawiera indeks elementu źródłowego. Następnie funkcja reprezentowana przez resultSelector jest wywoływana dla każdego elementu w każdej sekwencji pośredniej. Wynikowe wartości są łączone w jedną, jednowymiarową sekwencję.

Dotyczy

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

Źródło:
Queryable.cs
Źródło:
Queryable.cs
Źródło:
Queryable.cs

Projektuje każdy element sekwencji do elementu IEnumerable<T> i łączy wynikowe sekwencje w jedną sekwencję.

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)

Parametry typu

TSource

Typ elementów elementu source.

TResult

Typ elementów sekwencji zwróconych przez funkcję reprezentowaną przez selectorfunkcję .

Parametry

source
IQueryable<TSource>

Sekwencja wartości do projektu.

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

Funkcja projekcji, która ma być stosowana do każdego elementu.

Zwraca

IQueryable<TResult>

Element IQueryable<T> , którego elementy są wynikiem wywołania funkcji projekcji jeden do wielu w każdym elemecie sekwencji wejściowej.

Wyjątki

source lub selector to null.

Przykłady

W poniższym przykładzie kodu pokazano, jak wykonać SelectMany<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,IEnumerable<TResult>>>) projekcję jeden do wielu na tablicy.

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

Uwagi

Ta metoda ma co najmniej jeden parametr typu Expression<TDelegate> , którego argument type jest jednym z Func<T,TResult> typów. W przypadku tych parametrów można przekazać wyrażenie lambda i zostanie skompilowane do elementu Expression<TDelegate>.

Metoda SelectMany<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,IEnumerable<TResult>>>) generuje element MethodCallExpression , który reprezentuje wywołanie SelectMany<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,IEnumerable<TResult>>>) siebie jako skonstruowaną metodę ogólną. Następnie przekazuje MethodCallExpression element do CreateQuery(Expression) metody reprezentowanej IQueryProvider przez Provider właściwość parametru source .

Zachowanie zapytania, które występuje w wyniku wykonania drzewa wyrażeń, które reprezentuje wywołanie SelectMany<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,IEnumerable<TResult>>>) , zależy od implementacji typu parametru source . Oczekiwane zachowanie polega na tym, że wywołuje selector on każdy element source , aby projektować go w formie wyliczalnej. Następnie łączy wyliczone wyniki w jedną, jednowymiarową sekwencję.

Dotyczy

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

Źródło:
Queryable.cs
Źródło:
Queryable.cs
Źródło:
Queryable.cs

Projektuje każdy element sekwencji do elementu IEnumerable<T> i łączy wynikowe sekwencje w jedną sekwencję. Indeks każdego elementu źródłowego jest używany w przewidywanej formie tego elementu.

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)

Parametry typu

TSource

Typ elementów elementu source.

TResult

Typ elementów sekwencji zwróconych przez funkcję reprezentowaną przez selectorfunkcję .

Parametry

source
IQueryable<TSource>

Sekwencja wartości do projektu.

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

Funkcja projekcji, która ma być stosowana do każdego elementu; drugi parametr tej funkcji reprezentuje indeks elementu źródłowego.

Zwraca

IQueryable<TResult>

Element IQueryable<T> , którego elementy są wynikiem wywołania funkcji projekcji jeden do wielu w każdym elemecie sekwencji wejściowej.

Wyjątki

source lub selector to null.

Przykłady

W poniższym przykładzie kodu pokazano, jak wykonać SelectMany<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,Int32,IEnumerable<TResult>>>) projekcję jeden do wielu w tablicy i użyć indeksu każdego elementu źródłowego.

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

Uwagi

Ta metoda ma co najmniej jeden parametr typu Expression<TDelegate> , którego argument type jest jednym z Func<T,TResult> typów. W przypadku tych parametrów można przekazać wyrażenie lambda i zostanie skompilowane do elementu Expression<TDelegate>.

Metoda SelectMany<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,Int32,IEnumerable<TResult>>>) generuje element MethodCallExpression , który reprezentuje wywołanie SelectMany<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,Int32,IEnumerable<TResult>>>) siebie jako skonstruowaną metodę ogólną. Następnie przekazuje MethodCallExpression element do CreateQuery(Expression) metody reprezentowanej IQueryProvider przez Provider właściwość parametru source .

Zachowanie zapytania, które występuje w wyniku wykonania drzewa wyrażeń, które reprezentuje wywołanie SelectMany<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,Int32,IEnumerable<TResult>>>) , zależy od implementacji typu parametru source . Oczekiwane zachowanie polega na tym, że wywołuje selector on każdy element source , aby projektować go w formie wyliczalnej. Każdy wynik wyliczalny zawiera indeks elementu źródłowego. Następnie łączy wyliczone wyniki w jedną, jednowymiarową sekwencję.

Dotyczy