Enumerable.SelectMany Metódus
Definíció
Fontos
Egyes információk olyan, kiadás előtti termékekre vonatkoznak, amelyek a kiadásig még jelentősen módosulhatnak. A Microsoft nem vállal kifejezett vagy törvényi garanciát az itt megjelenő információért.
Egy sorozat egyes elemeit egy-egy IEnumerable<T> sorozatra alakítja, és az eredményül kapott sorozatokat egy sorozatba simítja.
Túlterhelések
| Name | Description |
|---|---|
| SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>) |
Egy sorozat egyes elemeit egy-egy IEnumerable<T>sorozatra kivetíti, az eredményül kapott sorozatokat egy sorozatba alakítja, és meghív egy eredményválasztó függvényt az egyes elemeken. |
| SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>) |
Egy sorozat egyes elemeit egy-egy IEnumerable<T>sorozatra kivetíti, az eredményül kapott sorozatokat egy sorozatba alakítja, és meghív egy eredményválasztó függvényt az egyes elemeken. Az egyes forráselemek indexét az elem köztes, előre jelzett formájában használja a rendszer. |
| SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>) |
Egy sorozat egyes elemeit egy-egy IEnumerable<T> sorozatra alakítja, és az eredményül kapott sorozatokat egy sorozatba simítja. |
| SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>) |
A sorozat egyes elemeit egy IEnumerable<T>- és egybesimítja, és az eredményül kapott sorozatokat egy sorozattá alakítja. Az egyes forráselemek indexét az elem előre jelzett formájában használja a rendszer. |
SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>)
- Forrás:
- SelectMany.cs
- Forrás:
- SelectMany.cs
- Forrás:
- SelectMany.cs
- Forrás:
- SelectMany.cs
- Forrás:
- SelectMany.cs
Egy sorozat egyes elemeit egy-egy IEnumerable<T>sorozatra kivetíti, az eredményül kapott sorozatokat egy sorozatba alakítja, és meghív egy eredményválasztó függvényt az egyes elemeken.
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)
Típusparaméterek
- TSource
A . elemeinek sourcetípusa.
- TCollection
Az összegyűjtött collectionSelectorköztes elemek típusa.
- TResult
Az eredményként kapott sorozat elemeinek típusa.
Paraméterek
- source
- IEnumerable<TSource>
A projekthez tartozó értékek sorozata.
- collectionSelector
- Func<TSource,IEnumerable<TCollection>>
A bemeneti sorozat minden elemére alkalmazandó átalakítási függvény.
- resultSelector
- Func<TSource,TCollection,TResult>
A köztes sorozat minden elemére alkalmazandó átalakítási függvény.
Válaszok
Egy IEnumerable<T> olyan elem, amelynek elemei az egy-a-többhöz átalakítási függvény collectionSelector meghívásának source eredménye, majd az egyes sorozatelemek és a hozzájuk tartozó forráselem egy eredményelemhez való hozzárendelése.
Kivételek
source vagy collectionSelectorresultSelector az nullis.
Példák
Az alábbi példakód bemutatja, hogyan hajthat SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>) végre egy-a-többhöz vetítést egy tömbön, és hogyan használhat eredményválasztó függvényt, hogy a forrásütemezés minden megfelelő elemét a végső hívás Selecthatókörében tartsa.
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
Megjegyzések
Ezt a metódust halasztott végrehajtással implementáljuk. Az azonnali visszatérési érték egy objektum, amely a művelet végrehajtásához szükséges összes információt tárolja. A metódus által képviselt lekérdezés csak akkor lesz végrehajtva, ha az objektumot a GetEnumerator metódus közvetlen meghívásával vagy foreach c# vagy For Each használatával Visual Basic.
Ez SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>) a módszer akkor hasznos, ha meg kell őriznie a lekérdezési logika hatókörében source lévő elemeket, amelyek a hívás után következnek SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>)be. A példakódért tekintse meg a Példa szakaszt. Ha kétirányú kapcsolat áll fenn a típusobjektumok TSource és a típusobjektumok TCollectionközött, azaz ha egy típusú TCollection objektum tulajdonságot biztosít az TSource azt előállító objektum lekéréséhez, akkor nincs szükség a túlterhelésére SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>). Ehelyett használhatja az objektumot, és visszatérhet SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>) az TSource objektumhoz az TCollection objektumon keresztül.
A lekérdezési kifejezés szintaxisában minden from záradék (C#) vagy From záradék (Visual Basic) a kezdeti után SelectMany meghívására fordít.
Lásd még
A következőre érvényes:
SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>)
- Forrás:
- SelectMany.cs
- Forrás:
- SelectMany.cs
- Forrás:
- SelectMany.cs
- Forrás:
- SelectMany.cs
- Forrás:
- SelectMany.cs
Egy sorozat egyes elemeit egy-egy IEnumerable<T>sorozatra kivetíti, az eredményül kapott sorozatokat egy sorozatba alakítja, és meghív egy eredményválasztó függvényt az egyes elemeken. Az egyes forráselemek indexét az elem köztes, előre jelzett formájában használja a rendszer.
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)
Típusparaméterek
- TSource
A . elemeinek sourcetípusa.
- TCollection
Az összegyűjtött collectionSelectorköztes elemek típusa.
- TResult
Az eredményként kapott sorozat elemeinek típusa.
Paraméterek
- source
- IEnumerable<TSource>
A projekthez tartozó értékek sorozata.
- collectionSelector
- Func<TSource,Int32,IEnumerable<TCollection>>
Az egyes forráselemekre alkalmazandó átalakító függvény; a függvény második paramétere a forráselem indexét jelöli.
- resultSelector
- Func<TSource,TCollection,TResult>
A köztes sorozat minden elemére alkalmazandó átalakítási függvény.
Válaszok
Egy IEnumerable<T> olyan elem, amelynek elemei az egy-a-többhöz átalakítási függvény collectionSelector meghívásának source eredménye, majd az egyes sorozatelemek és a hozzájuk tartozó forráselem egy eredményelemhez való hozzárendelése.
Kivételek
source vagy collectionSelectorresultSelector az nullis.
Megjegyzések
Ezt a metódust halasztott végrehajtással implementáljuk. Az azonnali visszatérési érték egy objektum, amely a művelet végrehajtásához szükséges összes információt tárolja. A metódus által képviselt lekérdezés csak akkor lesz végrehajtva, ha az objektumot a GetEnumerator metódus közvetlen meghívásával vagy foreach c# vagy For Each használatával Visual Basic.
Ez SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>) a módszer akkor hasznos, ha meg kell őriznie a lekérdezési logika hatókörében source lévő elemeket, amelyek a hívás után következnek SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>)be. A példakódért tekintse meg a Példa szakaszt. Ha kétirányú kapcsolat áll fenn a típusobjektumok TSource és a típusobjektumok TCollectionközött, azaz ha egy típusú TCollection objektum tulajdonságot biztosít az TSource azt előállító objektum lekéréséhez, akkor nincs szükség a túlterhelésére SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>). Ehelyett használhatja az objektumot, és visszatérhet SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>) az TSource objektumhoz az TCollection objektumon keresztül.
A következőre érvényes:
SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>)
- Forrás:
- SelectMany.cs
- Forrás:
- SelectMany.cs
- Forrás:
- SelectMany.cs
- Forrás:
- SelectMany.cs
- Forrás:
- SelectMany.cs
Egy sorozat egyes elemeit egy-egy IEnumerable<T> sorozatra alakítja, és az eredményül kapott sorozatokat egy sorozatba simítja.
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)
Típusparaméterek
- TSource
A . elemeinek sourcetípusa.
- TResult
A függvény által selectorvisszaadott sorozat elemeinek típusa.
Paraméterek
- source
- IEnumerable<TSource>
A projekthez tartozó értékek sorozata.
- selector
- Func<TSource,IEnumerable<TResult>>
Az egyes elemekre alkalmazandó átalakítási függvény.
Válaszok
Egy IEnumerable<T> olyan elem, amelynek elemei az egy-a-többhöz átalakítási függvény meghívásának eredménye a bemeneti sorozat egyes elemein.
Kivételek
source vagy selector az null.
Példák
Az alábbi példakód bemutatja, hogyan hajthat SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>) végre egy egy-a-többhöz vetítést egy tömbön.
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
Megjegyzések
Ezt a metódust halasztott végrehajtással implementáljuk. Az azonnali visszatérési érték egy objektum, amely a művelet végrehajtásához szükséges összes információt tárolja. A metódus által képviselt lekérdezés csak akkor lesz végrehajtva, ha az objektumot a GetEnumerator metódus közvetlen meghívásával vagy foreach c# vagy For Each használatával Visual Basic.
A SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>) metódus számba veszi a bemeneti sorrendet, átalakító függvényt használ az egyes elemek egy IEnumerable<T>adott elemhez való leképezéséhez, majd számba veszi és megadja az egyes objektumok IEnumerable<T> elemeit. Ez azt jelent, hogy a függvény minden egyes eleméhez sourcemeghívja a függvényt, selector és egy értéksorozatot ad vissza.
SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>) ezután a gyűjtemények kétdimenziós gyűjteményét egydimenzióssá IEnumerable<T> alakítja, és visszaadja. Ha például egy lekérdezés SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>) használatával kéri le a rendeléseket (Order típusú) az adatbázis minden egyes ügyfele számára, az eredmény IEnumerable<Order> C# vagy IEnumerable(Of Order) típusú Visual Basic. Ha a lekérdezés ehelyett Select használatával szerzi be a rendeléseket, a rendszer nem kombinálja a rendelésgyűjteményeket, és az eredmény IEnumerable<List<Order>> C# vagy IEnumerable(Of List(Of Order)) típusú Visual Basic.
A lekérdezési kifejezés szintaxisában minden from záradék (C#) vagy From záradék (Visual Basic) a kezdeti után SelectMany meghívására fordít.
Lásd még
A következőre érvényes:
SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>)
- Forrás:
- SelectMany.cs
- Forrás:
- SelectMany.cs
- Forrás:
- SelectMany.cs
- Forrás:
- SelectMany.cs
- Forrás:
- SelectMany.cs
A sorozat egyes elemeit egy IEnumerable<T>- és egybesimítja, és az eredményül kapott sorozatokat egy sorozattá alakítja. Az egyes forráselemek indexét az elem előre jelzett formájában használja a rendszer.
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)
Típusparaméterek
- TSource
A . elemeinek sourcetípusa.
- TResult
A függvény által selectorvisszaadott sorozat elemeinek típusa.
Paraméterek
- source
- IEnumerable<TSource>
A projekthez tartozó értékek sorozata.
- selector
- Func<TSource,Int32,IEnumerable<TResult>>
Az egyes forráselemekre alkalmazandó átalakító függvény; a függvény második paramétere a forráselem indexét jelöli.
Válaszok
Egy IEnumerable<T> olyan elem, amelynek elemei az egy-a-többhöz átalakítási függvény meghívásának eredménye egy bemeneti sorozat egyes elemein.
Kivételek
source vagy selector az null.
Példák
Az alábbi példakód bemutatja, hogyan lehet SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>) egy egy-a-többhöz vetítést végrehajtani egy tömbön, és hogyan használhatja az egyes külső elemek indexét.
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
Megjegyzések
Ezt a metódust halasztott végrehajtással implementáljuk. Az azonnali visszatérési érték egy objektum, amely a művelet végrehajtásához szükséges összes információt tárolja. A metódus által képviselt lekérdezés csak akkor lesz végrehajtva, ha az objektumot a GetEnumerator metódus közvetlen meghívásával vagy foreach c# vagy For Each használatával Visual Basic.
A SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>) metódus számba veszi a bemeneti sorrendet, átalakító függvényt használ az egyes elemek egy IEnumerable<T>adott elemhez való leképezéséhez, majd számba veszi és megadja az egyes objektumok IEnumerable<T> elemeit. Ez azt jelent, hogy a függvény minden egyes eleméhez sourcemeghívja a függvényt, selector és egy értéksorozatot ad vissza.
SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>) ezután a gyűjtemények kétdimenziós gyűjteményét egydimenzióssá IEnumerable<T> alakítja, és visszaadja. Ha például egy lekérdezés SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>) használatával kéri le a rendeléseket (Order típusú) az adatbázis minden egyes ügyfele számára, az eredmény IEnumerable<Order> C# vagy IEnumerable(Of Order) típusú Visual Basic. Ha a lekérdezés ehelyett Select használatával szerzi be a rendeléseket, a rendszer nem kombinálja a rendelésgyűjteményeket, és az eredmény IEnumerable<List<Order>> C# vagy IEnumerable(Of List(Of Order)) típusú Visual Basic.
Az első argumentum, amely selector a feldolgozandó elemet jelöli. A második argumentum, amely selector az adott elem nulla alapú indexét jelöli a forrásütemezésben. Ez akkor lehet hasznos, ha az elemek ismert sorrendben vannak, és például egy adott index egy elemével szeretne valamit csinálni. Akkor is hasznos lehet, ha egy vagy több elem indexét szeretné lekérni.