Enumerable.SelectMany Metoda

Definicja

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

Przeciążenia

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

Projektuje każdy element sekwencji do IEnumerable<T>elementu , spłaszcza wynikowe sekwencje w jedną sekwencję i wywołuje funkcję selektora wyników w każdym elemecie.

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

Projektuje każdy element sekwencji do IEnumerable<T>elementu , spłaszcza wynikowe sekwencje w jedną sekwencję i wywołuje funkcję selektora wyników w każdym elemecie. Indeks każdego elementu źródłowego jest używany w przewidywanej formie pośredniej tego elementu.

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

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

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

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

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

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

Projektuje każdy element sekwencji do IEnumerable<T>elementu , spłaszcza wynikowe sekwencje w jedną sekwencję i wywołuje funkcję selektora wyników w każdym elemecie.

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)

Parametry typu

TSource

Typ elementów elementu source.

TCollection

Typ elementów pośrednich zebranych przez collectionSelectorelement .

TResult

Typ elementów wynikowej sekwencji.

Parametry

source
IEnumerable<TSource>

Sekwencja wartości do projektu.

collectionSelector
Func<TSource,IEnumerable<TCollection>>

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

resultSelector
Func<TSource,TCollection,TResult>

Funkcja przekształcania, która ma być stosowana do każdego elementu sekwencji pośredniej.

Zwraca

IEnumerable<TResult>

Element IEnumerable<T> , którego elementy są wynikiem wywołania funkcji collectionSelector przekształcania jeden do wielu w każdym elemecie, source a następnie mapowania każdego z tych elementów sekwencji i odpowiadającego im elementu źródłowego na element wynikowy.

Wyjątki

source lub collectionSelectorresultSelector jest null.

Przykłady

W poniższym przykładzie kodu pokazano, jak wykonać SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>) projekcję jeden do wielu w tablicy i użyć funkcji selektora wyników, aby zachować każdy odpowiedni element z sekwencji źródłowej w zakresie ostatniego wywołania do 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.
    Console.WriteLine(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

Uwagi

Ta metoda jest implementowana za pomocą odroczonego wykonania. Bezpośrednio zwracana wartość jest obiektem, który przechowuje wszystkie informacje wymagane do wykonania akcji. Zapytanie reprezentowane przez tę metodę nie jest wykonywane, dopóki obiekt nie zostanie wyliczony, wywołując metodę GetEnumerator bezpośrednio lub używając w foreach języku C# lub For Each w Visual Basic.

Metoda jest przydatna SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>) , gdy trzeba zachować elementy source w zakresie logiki zapytań, która występuje po wywołaniu metody SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>). Zobacz sekcję Przykładowy przykład kodu. Jeśli istnieje dwukierunkowa relacja między obiektami typu i obiektami typu TSourceTCollection, oznacza to, że jeśli obiekt typu TCollection zapewnia właściwość pobierania TSource obiektu, który go wyprodukował, nie potrzebujesz tego przeciążenia SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>). Zamiast tego możesz użyć SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>) obiektu i przejść z powrotem do TSource obiektu za pośrednictwem TCollection obiektu.

W składni wyrażenia zapytania każda from klauzula (C#) lub From klauzula (Visual Basic) po początkowym translacji na wywołanie SelectManyelementu .

Zobacz też

Dotyczy

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

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

Projektuje każdy element sekwencji do IEnumerable<T>elementu , spłaszcza wynikowe sekwencje w jedną sekwencję i wywołuje funkcję selektora wyników w każdym elemecie. Indeks każdego elementu źródłowego jest używany w przewidywanej formie pośredniej tego elementu.

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)

Parametry typu

TSource

Typ elementów elementu source.

TCollection

Typ elementów pośrednich zebranych przez collectionSelectorelement .

TResult

Typ elementów wynikowej sekwencji.

Parametry

source
IEnumerable<TSource>

Sekwencja wartości do projektu.

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

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

resultSelector
Func<TSource,TCollection,TResult>

Funkcja przekształcania, która ma być stosowana do każdego elementu sekwencji pośredniej.

Zwraca

IEnumerable<TResult>

Element IEnumerable<T> , którego elementy są wynikiem wywołania funkcji collectionSelector przekształcania jeden do wielu w każdym elemecie, source a następnie mapowania każdego z tych elementów sekwencji i odpowiadającego im elementu źródłowego na element wynikowy.

Wyjątki

source lub collectionSelectorresultSelector jest null.

Uwagi

Ta metoda jest implementowana za pomocą odroczonego wykonania. Bezpośrednio zwracana wartość jest obiektem, który przechowuje wszystkie informacje wymagane do wykonania akcji. Zapytanie reprezentowane przez tę metodę nie jest wykonywane, dopóki obiekt nie zostanie wyliczony, wywołując metodę GetEnumerator bezpośrednio lub używając w foreach języku C# lub For Each w Visual Basic.

Metoda jest przydatna SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>) , gdy trzeba zachować elementy source w zakresie logiki zapytań, która występuje po wywołaniu metody SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>). Zobacz sekcję Przykładowy przykład kodu. Jeśli istnieje dwukierunkowa relacja między obiektami typu i obiektami typu TSourceTCollection, oznacza to, że jeśli obiekt typu TCollection zapewnia właściwość pobierania TSource obiektu, który go wyprodukował, nie potrzebujesz tego przeciążenia SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>). Zamiast tego możesz użyć SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>) obiektu i przejść z powrotem do TSource obiektu za pośrednictwem TCollection obiektu.

Dotyczy

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

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

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

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)

Parametry typu

TSource

Typ elementów elementu source.

TResult

Typ elementów sekwencji zwróconych przez selectorelement .

Parametry

source
IEnumerable<TSource>

Sekwencja wartości do projektu.

selector
Func<TSource,IEnumerable<TResult>>

Funkcja transformacji do zastosowania do każdego elementu.

Zwraca

IEnumerable<TResult>

Element IEnumerable<T> , którego elementy są wynikiem wywołania funkcji przekształcania jeden do wielu w każdym elemecie sekwencji danych wejściowych.

Wyjątki

source lub selector to null.

Przykłady

W poniższym przykładzie kodu pokazano, jak wykonać SelectMany<TSource,TResult>(IEnumerable<TSource>, 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.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.
    Console.WriteLine(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 jest implementowana za pomocą odroczonego wykonania. Bezpośrednio zwracana wartość jest obiektem, który przechowuje wszystkie informacje wymagane do wykonania akcji. Zapytanie reprezentowane przez tę metodę nie jest wykonywane, dopóki obiekt nie zostanie wyliczony, wywołując metodę GetEnumerator bezpośrednio lub używając w foreach języku C# lub For Each w Visual Basic.

Metoda SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>) wylicza sekwencję wejściową, używa funkcji transform, aby zamapować każdy element na IEnumerable<T>element , a następnie wylicza i zwraca elementy każdego takiego IEnumerable<T> obiektu. Oznacza to, że dla każdego elementu sourceselector elementu jest wywoływana, a zwracana jest sekwencja wartości. SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>) następnie spłaszcza tę dwuwymiarową kolekcję kolekcji w jednowymiarową IEnumerable<T> i zwraca ją. Jeśli na przykład zapytanie używa SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>) polecenia do uzyskania zamówień (typu Order) dla każdego klienta w bazie danych, wynik jest typu IEnumerable<Order> w języku C# lub IEnumerable(Of Order) w Visual Basic. Jeśli zamiast tego zapytanie używa Select polecenia do uzyskania zamówień, kolekcja kolekcji zamówień nie jest połączona, a wynik jest typu IEnumerable<List<Order>> w języku C# lub IEnumerable(Of List(Of Order)) w Visual Basic.

W składni wyrażenia zapytania każda from klauzula (C#) lub From klauzula (Visual Basic) po początkowym translacji na wywołanie SelectManyelementu .

Zobacz też

Dotyczy

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

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

Projektuje każdy element sekwencji do IEnumerable<T>elementu i spłaszcza 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::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)

Parametry typu

TSource

Typ elementów elementu source.

TResult

Typ elementów sekwencji zwróconych przez selectorelement .

Parametry

source
IEnumerable<TSource>

Sekwencja wartości do projektu.

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

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

Zwraca

IEnumerable<TResult>

Element IEnumerable<T> , którego elementy są wynikiem wywołania funkcji przekształcania 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>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>) projekcję jeden do wielu na tablicy i użyć indeksu każdego elementu zewnętrznego.

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

Uwagi

Ta metoda jest implementowana za pomocą odroczonego wykonania. Bezpośrednio zwracana wartość jest obiektem, który przechowuje wszystkie informacje wymagane do wykonania akcji. Zapytanie reprezentowane przez tę metodę nie jest wykonywane, dopóki obiekt nie zostanie wyliczony, wywołując metodę GetEnumerator bezpośrednio lub używając w foreach języku C# lub For Each w Visual Basic.

Metoda SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>) wylicza sekwencję wejściową, używa funkcji transform, aby zamapować każdy element na IEnumerable<T>element , a następnie wylicza i zwraca elementy każdego takiego IEnumerable<T> obiektu. Oznacza to, że dla każdego elementu sourceselector elementu jest wywoływana, a zwracana jest sekwencja wartości. SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>) następnie spłaszcza tę dwuwymiarową kolekcję kolekcji w jednowymiarową IEnumerable<T> i zwraca ją. Jeśli na przykład zapytanie używa SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>) polecenia do uzyskania zamówień (typu Order) dla każdego klienta w bazie danych, wynik jest typu IEnumerable<Order> w języku C# lub IEnumerable(Of Order) w Visual Basic. Jeśli zamiast tego zapytanie używa Select polecenia do uzyskania zamówień, kolekcja kolekcji zamówień nie jest połączona, a wynik jest typu IEnumerable<List<Order>> w języku C# lub IEnumerable(Of List(Of Order)) w Visual Basic.

Pierwszy argument selector reprezentujący element do przetworzenia. Drugi argument selector reprezentujący indeks zerowy tego elementu w sekwencji źródłowej. Może to być przydatne, jeśli elementy są w znanej kolejności i chcesz zrobić coś z elementem w określonym indeksie, na przykład. Może to być również przydatne, jeśli chcesz pobrać indeks jednego lub większej liczby elementów.

Dotyczy