Enumerable.SelectManyEnumerable.SelectManyEnumerable.SelectManyEnumerable.SelectMany Method

多載

 SelectMany(IEnumerable, Func>, Func) SelectMany(IEnumerable, Func>, Func) SelectMany(IEnumerable, Func>, Func) SelectMany(IEnumerable, Func>, Func) 將序列的每個項目投影為 IEnumerable、將產生的序列簡化成單一序列，並對其中的每個項目叫用結果選取器函式。Projects each element of a sequence to an IEnumerable, flattens the resulting sequences into one sequence, and invokes a result selector function on each element therein. SelectMany(IEnumerable, Func>, Func) SelectMany(IEnumerable, Func>, Func) SelectMany(IEnumerable, Func>, Func) SelectMany(IEnumerable, Func>, Func) 將序列的每個項目投影為 IEnumerable、將產生的序列簡化成單一序列，並對其中的每個項目叫用結果選取器函式。Projects each element of a sequence to an IEnumerable, flattens the resulting sequences into one sequence, and invokes a result selector function on each element therein. 各來源項目的索引是在該項目的中繼投影表單中使用。The index of each source element is used in the intermediate projected form of that element. SelectMany(IEnumerable, Func>) SelectMany(IEnumerable, Func>) SelectMany(IEnumerable, Func>) SelectMany(IEnumerable, Func>) 將序列的每個項目都投影成 IEnumerable，並將產生的序列簡化成單一序列。Projects each element of a sequence to an IEnumerable and flattens the resulting sequences into one sequence. SelectMany(IEnumerable, Func>) SelectMany(IEnumerable, Func>) SelectMany(IEnumerable, Func>) SelectMany(IEnumerable, Func>) 將序列的每個項目都投影成 IEnumerable，並將產生的序列簡化成單一序列。Projects each element of a sequence to an IEnumerable, and flattens the resulting sequences into one sequence. 各來源項目的索引是在該項目的投影表單中使用。The index of each source element is used in the projected form of that element.

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

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

類型參數

TSource

`source` 項目的類型。The type of the elements of `source`.

TCollection

`collectionSelector` 所收集之中繼項目的型別。The type of the intermediate elements collected by `collectionSelector`.

TResult

參數

source
IEnumerable<TSource>

collectionSelector
Func<TSource,IEnumerable<TCollection>>

resultSelector
Func<TSource,TCollection,TResult>

傳回

IEnumerable<TResult>

IEnumerable<T>，其項目是執行下列動作後所產生的結果：對 `collectionSelector` 的各個項目叫用一對多轉換函式 `source`，然後再將每個序列項目及其對應的來源項目對應到結果項目。An IEnumerable<T> whose elements are the result of invoking the one-to-many transform function `collectionSelector` on each element of `source` and then mapping each of those sequence elements and their corresponding source element to a result element.

例外狀況

`source``collectionSelector``resultSelector``null``source` or `collectionSelector` or `resultSelector` is `null`.

範例

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

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

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

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

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

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

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

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

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

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

備註

The SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>) method is useful when you have to keep the elements of `source` in scope for query logic that occurs after the call to SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>). 如需程式碼範例，請參閱＜範例＞一節。See the Example section for a code example. 如果`TSource`類型的物件與類型`TCollection`的物件之間有雙向關聯性, 也就是, 如果類型`TCollection`的物件提供屬性來抓取產生它的`TSource`物件, 您就不需要這麼做。的SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>)多載。If there is a bidirectional relationship between objects of type `TSource` and objects of type `TCollection`, that is, if an object of type `TCollection` provides a property to retrieve the `TSource` object that produced it, you do not need this overload of SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>). 相反地, 您可以SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>)使用, 並透過`TCollection`物件`TSource`流覽回到物件。Instead, you can use SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>) and navigate back to the `TSource` object through the `TCollection` object.

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

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

類型參數

TSource

`source` 項目的類型。The type of the elements of `source`.

TCollection

`collectionSelector` 所收集之中繼項目的型別。The type of the intermediate elements collected by `collectionSelector`.

TResult

參數

source
IEnumerable<TSource>

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

resultSelector
Func<TSource,TCollection,TResult>

傳回

IEnumerable<TResult>

IEnumerable<T>，其項目是執行下列動作後所產生的結果：對 `collectionSelector` 的各個項目叫用一對多轉換函式 `source`，然後再將每個序列項目及其對應的來源項目對應到結果項目。An IEnumerable<T> whose elements are the result of invoking the one-to-many transform function `collectionSelector` on each element of `source` and then mapping each of those sequence elements and their corresponding source element to a result element.

例外狀況

`source``collectionSelector``resultSelector``null``source` or `collectionSelector` or `resultSelector` is `null`.

備註

The SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>) method is useful when you have to keep the elements of `source` in scope for query logic that occurs after the call to SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>). 如需程式碼範例，請參閱＜範例＞一節。See the Example section for a code example. 如果`TSource`類型的物件與類型`TCollection`的物件之間有雙向關聯性, 也就是, 如果類型`TCollection`的物件提供屬性來抓取產生它的`TSource`物件, 您就不需要這麼做。的SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>)多載。If there is a bidirectional relationship between objects of type `TSource` and objects of type `TCollection`, that is, if an object of type `TCollection` provides a property to retrieve the `TSource` object that produced it, you do not need this overload of SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>). 相反地, 您可以SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>)使用, 並透過`TCollection`物件`TSource`流覽回到物件。Instead, you can use SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>) and navigate back to the `TSource` object through the `TCollection` object.

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

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

類型參數

TSource

`source` 項目的類型。The type of the elements of `source`.

TResult

`selector` 所傳回序列之項目的型別。The type of the elements of the sequence returned by `selector`.

參數

source
IEnumerable<TSource>

selector
Func<TSource,IEnumerable<TResult>>

傳回

IEnumerable<TResult>

IEnumerable<T>，其項目是在輸入序列的各個項目上叫用一對多轉換函式所產生的結果。An IEnumerable<T> whose elements are the result of invoking the one-to-many transform function on each element of the input sequence.

例外狀況

`source``selector``null``source` or `selector` is `null`.

範例

``````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()
IEnumerable<List<String>> query2 =
petOwners.Select(petOwner => petOwner.Pets);

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

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

/*
This code produces the following output:

Using SelectMany():
Scruffy
Sam
Walker
Sugar
Scratches
Diesel

Using Select():
Scruffy
Sam

Walker
Sugar

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

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

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

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

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

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

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

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

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

類型參數

TSource

`source` 項目的類型。The type of the elements of `source`.

TResult

`selector` 所傳回序列之項目的型別。The type of the elements of the sequence returned by `selector`.

參數

source
IEnumerable<TSource>

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

傳回

IEnumerable<TResult>

IEnumerable<T>，其項目是對輸入序列中各個項目叫用一對多轉換函式後所產生的結果。An IEnumerable<T> whose elements are the result of invoking the one-to-many transform function on each element of an input sequence.

例外狀況

`source``selector``null``source` or `selector` is `null`.

範例

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

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

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

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

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

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

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

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

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