Enumerable.SelectMany 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將序列的每個專案投影到 IEnumerable<T>,並將產生的序列扁平化成一個序列。
多載
| 名稱 | Description |
|---|---|
| SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>) |
將序列的每個專案投影到 IEnumerable<T>,將產生的序列扁平化成一個序列,並在其中的每個元素上叫用結果選取器函式。 |
| SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>) |
將序列的每個專案投影到 IEnumerable<T>,將產生的序列扁平化成一個序列,並在其中的每個元素上叫用結果選取器函式。 每個來源專案的索引會用於該專案的中繼投影形式。 |
| SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>) |
將序列的每個專案投影到 IEnumerable<T>,並將產生的序列扁平化成一個序列。 |
| SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>) |
將序列的每個專案投影至 IEnumerable<T>,並將產生的序列扁平化成一個序列。 每個來源專案的索引會以該專案的投影形式使用。 |
SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>)
將序列的每個專案投影到 IEnumerable<T>,將產生的序列扁平化成一個序列,並在其中的每個元素上叫用結果選取器函式。
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的類型。
- TCollection
由 收集 collectionSelector的中間元素類型。
- TResult
結果序列中元素的類型。
參數
- source
- IEnumerable<TSource>
一連串的價值要投射。
- collectionSelector
- Func<TSource,IEnumerable<TCollection>>
一個轉換函數,應用於輸入序列中的每個元素。
- resultSelector
- Func<TSource,TCollection,TResult>
一個轉換函數,應用於中間序列的每個元素。
傳回
其IEnumerable<T>元素是對每個元素collectionSelector調用一對多轉換函source數,然後將這些序列元素及其對應的來源元素映射到結果元素的結果。
例外狀況
source或collectionSelectorresultSelector是 null。
範例
以下程式碼範例示範如何對 SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>) 陣列進行一對多投影,並使用結果選擇函數保持來源序列中每個對應元素在最終呼叫 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
備註
此方法透過延遲執行實現。 立即回傳值是一個物件,儲存執行動作所需的所有資訊。 此方法所代表的查詢,直到物件被枚舉,無論是直接呼叫其 GetEnumerator 方法,或是使用 C# 中的 foreach,或在 Visual Basic 中使用 For Each,才會執行。
當需要在查詢邏輯中保持 的SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>)元素在 的範圍內,以應付呼叫 之後source的查詢邏輯時,此SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>)方法非常有用。 請參閱範例章節以了解程式碼範例。 如果 型態TSource的物件與型別TCollection的物件之間存在雙向關係,也就是說,若型別TCollection的物件提供了取得產生它的物件的TSource屬性,則不需要這個過載。SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>) 相反地,你可以使用SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>)並透過TSource物件導航回物件TCollection。
在查詢表達式語法中,每個 from子句(C#)或 From子句(Visual Basic)後,都會轉換成 SelectMany 的調用。
另請參閱
適用於
SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>)
將序列的每個專案投影到 IEnumerable<T>,將產生的序列扁平化成一個序列,並在其中的每個元素上叫用結果選取器函式。 每個來源專案的索引會用於該專案的中繼投影形式。
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的類型。
- TCollection
由 收集 collectionSelector的中間元素類型。
- TResult
結果序列中元素的類型。
參數
- source
- IEnumerable<TSource>
一連串的價值要投射。
- collectionSelector
- Func<TSource,Int32,IEnumerable<TCollection>>
一個轉換函數,應用於每個來源元素;函數的第二個參數代表來源元素的索引。
- resultSelector
- Func<TSource,TCollection,TResult>
一個轉換函數,應用於中間序列的每個元素。
傳回
其IEnumerable<T>元素是對每個元素collectionSelector調用一對多轉換函source數,然後將這些序列元素及其對應的來源元素映射到結果元素的結果。
例外狀況
source或collectionSelectorresultSelector是 null。
備註
此方法透過延遲執行實現。 立即回傳值是一個物件,儲存執行動作所需的所有資訊。 此方法所代表的查詢,直到物件被枚舉,無論是直接呼叫其 GetEnumerator 方法,或是使用 C# 中的 foreach,或在 Visual Basic 中使用 For Each,才會執行。
當需要在查詢邏輯中保持 的SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>)元素在 的範圍內,以應付呼叫 之後source的查詢邏輯時,此SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>)方法非常有用。 請參閱範例章節以了解程式碼範例。 如果 型態TSource的物件與型別TCollection的物件之間存在雙向關係,也就是說,若型別TCollection的物件提供了取得產生它的物件的TSource屬性,則不需要這個過載。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>>)並透過TSource物件導航回物件TCollection。
適用於
SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>)
將序列的每個專案投影到 IEnumerable<T>,並將產生的序列扁平化成一個序列。
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的類型。
- TResult
序列中元素的類型由 返回 selector。
參數
- source
- IEnumerable<TSource>
一連串的價值要投射。
- selector
- Func<TSource,IEnumerable<TResult>>
一個轉換函數,可以套用到每個元素。
傳回
其 IEnumerable<T> 元素是對輸入序列中每個元素調用一對多轉換函數所產生的結果。
例外狀況
source 或 selector 為 null。
範例
以下程式碼範例示範如何使用 SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>) 對陣列進行一對多投影。
class PetOwner
{
public string Name { get; set; }
public List<String> Pets { get; set; }
}
public static void SelectManyEx1()
{
PetOwner[] petOwners =
{ new PetOwner { Name="Higa, Sidney",
Pets = new List<string>{ "Scruffy", "Sam" } },
new PetOwner { Name="Ashkenazi, Ronen",
Pets = new List<string>{ "Walker", "Sugar" } },
new PetOwner { Name="Price, Vernette",
Pets = new List<string>{ "Scratches", "Diesel" } } };
// Query using SelectMany().
IEnumerable<string> query1 = petOwners.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
備註
此方法透過延遲執行實現。 立即回傳值是一個物件,儲存執行動作所需的所有資訊。 此方法所代表的查詢,直到物件被枚舉,無論是直接呼叫其 GetEnumerator 方法,或是使用 C# 中的 foreach,或在 Visual Basic 中使用 For Each,才會執行。
該 SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>) 方法枚舉輸入序列,使用轉換函數將每個元素映射到 IEnumerable<T>,然後枚舉並產生每個此類 IEnumerable<T> 物件的元素。 也就是說,對 的sourceselector每個元素 ,都會被調用,並回傳一串值。
SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>) 然後將這個二維集合平面化為一維 IEnumerable<T> ,並返回它。 例如,若查詢使用 SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>) 取得資料庫中每位客戶的訂單(類型為 Order),結果在 C# 中為 IEnumerable<Order>,或在 Visual Basic 中為 IEnumerable(Of Order)。 若查詢改用 Select 取得順序,則順序集合不會合併,結果為 C# 中的 IEnumerable<List<Order>> 或 Visual Basic 中的 IEnumerable(Of List(Of Order))型。
在查詢表達式語法中,每個 from子句(C#)或 From子句(Visual Basic)後,都會轉換成 SelectMany 的調用。
另請參閱
適用於
SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>)
將序列的每個專案投影至 IEnumerable<T>,並將產生的序列扁平化成一個序列。 每個來源專案的索引會以該專案的投影形式使用。
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的類型。
- TResult
序列中元素的類型由 返回 selector。
參數
- source
- IEnumerable<TSource>
一連串的價值要投射。
- selector
- Func<TSource,Int32,IEnumerable<TResult>>
一個轉換函數,應用於每個來源元素;函數的第二個參數代表來源元素的索引。
傳回
其 IEnumerable<T> 元素是對輸入序列中每個元素調用一對多轉換函數所產生的結果。
例外狀況
source 或 selector 為 null。
範例
以下程式碼範例示範如何使用 SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>) 對陣列進行一對多投影,並使用每個外層元素的索引。
class PetOwner
{
public string Name { get; set; }
public List<string> Pets { get; set; }
}
public static void SelectManyEx2()
{
PetOwner[] petOwners =
{ new PetOwner { Name="Higa, Sidney",
Pets = new List<string>{ "Scruffy", "Sam" } },
new PetOwner { Name="Ashkenazi, Ronen",
Pets = new List<string>{ "Walker", "Sugar" } },
new PetOwner { Name="Price, Vernette",
Pets = new List<string>{ "Scratches", "Diesel" } },
new PetOwner { Name="Hines, Patrick",
Pets = new List<string>{ "Dusty" } } };
// 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
備註
此方法透過延遲執行實現。 立即回傳值是一個物件,儲存執行動作所需的所有資訊。 此方法所代表的查詢,直到物件被枚舉,無論是直接呼叫其 GetEnumerator 方法,或是使用 C# 中的 foreach,或在 Visual Basic 中使用 For Each,才會執行。
該 SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>) 方法枚舉輸入序列,使用轉換函數將每個元素映射到 IEnumerable<T>,然後枚舉並產生每個此類 IEnumerable<T> 物件的元素。 也就是說,對 的sourceselector每個元素 ,都會被調用,並回傳一串值。
SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>) 然後將這個二維集合平面化為一維 IEnumerable<T> ,並返回它。 例如,若查詢使用 SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>) 取得資料庫中每位客戶的訂單(類型為 Order),結果在 C# 中為 IEnumerable<Order>,或在 Visual Basic 中為 IEnumerable(Of Order)。 若查詢改用 Select 取得順序,則順序集合不會合併,結果為 C# 中的 IEnumerable<List<Order>> 或 Visual Basic 中的 IEnumerable(Of List(Of Order))型。
第一個 selector 參數代表要處理的元素。 第二個 selector 參數代表該元素在來源序列中以零為基礎的索引。 如果元素順序已知,且你想對某個特定索引的元素做某件事,這會很有用。 如果你想取得一個或多個元素的索引,它也很有用。