Enumerable.SelectMany 方法

定義

將序列的每個項目都投影成 IEnumerable<T>,並將產生的序列簡化成單一序列。

多載

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>)

來源:
SelectMany.cs
來源:
SelectMany.cs
來源:
SelectMany.cs

將序列的每個項目投影為 IEnumerable<T>、將產生的序列簡化成單一序列,並對其中的每個項目叫用結果選取器函式。

C#
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);

類型參數

TSource

source 項目的類型。

TCollection

collectionSelector 所收集之中繼項目的型別。

TResult

產生的序列之項目型別。

參數

source
IEnumerable<TSource>

要投影的值序列。

collectionSelector
Func<TSource,IEnumerable<TCollection>>

要套用到輸入序列中各個項目的轉換函式。

resultSelector
Func<TSource,TCollection,TResult>

要套用到中繼序列中各個項目的轉換函式。

傳回

IEnumerable<TResult>

IEnumerable<T>,其項目是執行下列動作後所產生的結果:對 collectionSelector 的各個項目叫用一對多轉換函式 source,然後再將每個序列項目及其對應的來源項目對應到結果項目。

例外狀況

sourcecollectionSelectorresultSelectornull

範例

下列程式代碼範例示範如何使用 SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>) 在數位上執行一對多投影,並使用結果選取器函式,將每個對應的專案從來源序列保留在範圍中,以供最終呼叫 Select

C#
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}

備註

這個方法是使用延後執行來實作。 立即傳回值是一個物件,會儲存執行動作所需的所有資訊。 除非直接呼叫其 GetEnumerator 方法或在 C# 或 foreachFor Each Visual Basic 中使用 來列舉對象,否則不會執行這個方法所代表的查詢。

當您必須針對在呼叫 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別的對象之間有雙向關聯性,也就是說,如果 類型的TCollectionTCollection物件提供屬性來擷取TSource產生的物件,則您不需要這個多載 。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 物件。

在查詢表達式語法中,初始子句轉譯為 的調用SelectMany之後,每個from子句都會 (C#) 或 From 子句 (Visual Basic) 。

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

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

來源:
SelectMany.cs
來源:
SelectMany.cs
來源:
SelectMany.cs

將序列的每個項目投影為 IEnumerable<T>、將產生的序列簡化成單一序列,並對其中的每個項目叫用結果選取器函式。 各來源項目的索引是在該項目的中繼投影表單中使用。

C#
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);

類型參數

TSource

source 項目的類型。

TCollection

collectionSelector 所收集之中繼項目的型別。

TResult

產生的序列之項目型別。

參數

source
IEnumerable<TSource>

要投影的值序列。

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

要套用到每個來源項目的轉換函式;此函式的第二個參數代表來源項目的索引。

resultSelector
Func<TSource,TCollection,TResult>

要套用到中繼序列中各個項目的轉換函式。

傳回

IEnumerable<TResult>

IEnumerable<T>,其項目是執行下列動作後所產生的結果:對 collectionSelector 的各個項目叫用一對多轉換函式 source,然後再將每個序列項目及其對應的來源項目對應到結果項目。

例外狀況

sourcecollectionSelectorresultSelectornull

備註

這個方法是使用延後執行來實作。 立即傳回值是一個物件,會儲存執行動作所需的所有資訊。 除非直接呼叫其 GetEnumerator 方法或在 C# 或 foreachFor Each Visual Basic 中使用 來列舉對象,否則不會執行這個方法所代表的查詢。

當您必須針對在呼叫 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別的對象之間有雙向關聯性,也就是說,如果 類型的TCollectionTCollection物件提供屬性來擷取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>>) 並透過 TCollection 物件巡覽回 TSource 物件。

適用於

.NET 9 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

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

來源:
SelectMany.cs
來源:
SelectMany.cs
來源:
SelectMany.cs

將序列的每個項目都投影成 IEnumerable<T>,並將產生的序列簡化成單一序列。

C#
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);

類型參數

TSource

source 項目的類型。

TResult

selector 所傳回序列之項目的型別。

參數

source
IEnumerable<TSource>

要投影的值序列。

selector
Func<TSource,IEnumerable<TResult>>

要套用至每個項目的轉換函式。

傳回

IEnumerable<TResult>

IEnumerable<T>,其項目是在輸入序列的各個項目上叫用一對多轉換函式所產生的結果。

例外狀況

sourceselectornull

範例

下列程式代碼範例示範如何使用 SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>) 在數位上執行一對多投影。

C#
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
*/

備註

這個方法是使用延後執行來實作。 立即傳回值是一個物件,會儲存執行動作所需的所有資訊。 除非直接呼叫其 GetEnumerator 方法或在 C# 或 foreachFor Each Visual Basic 中使用 來列舉對象,否則不會執行這個方法所代表的查詢。

方法 SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>) 會列舉輸入序列、使用轉換函式將每個元素對應至 IEnumerable<T>,然後列舉併產生每個這類 IEnumerable<T> 對象的元素。 也就是說,會針對的每個專案 source叫用 , selector 並傳回一連串的值。 SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>) 然後將這個二維集合的集合壓平合併成一維 IEnumerable<T> ,並傳回它。 例如,如果查詢使用 SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>) 來取得資料庫中每個客戶) 類型的 Order 訂單 (,則結果的類型 IEnumerable<Order> 為 C# 或 IEnumerable(Of Order) Visual Basic。 如果查詢改用 Select 來取得訂單,則不會合併訂單集合的集合,而且結果的類型 IEnumerable<List<Order>> 為 C# 或 IEnumerable(Of List(Of Order)) Visual Basic。

在查詢表達式語法中,初始子句轉譯為 的調用SelectMany之後,每個from子句都會 (C#) 或 From 子句 (Visual Basic) 。

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

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

來源:
SelectMany.cs
來源:
SelectMany.cs
來源:
SelectMany.cs

將序列的每個項目都投影成 IEnumerable<T>,並將產生的序列簡化成單一序列。 各來源項目的索引是在該項目的投影表單中使用。

C#
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);

類型參數

TSource

source 項目的類型。

TResult

selector 所傳回序列之項目的型別。

參數

source
IEnumerable<TSource>

要投影的值序列。

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

要套用到每個來源項目的轉換函式;此函式的第二個參數代表來源項目的索引。

傳回

IEnumerable<TResult>

IEnumerable<T>,其項目是對輸入序列中各個項目叫用一對多轉換函式後所產生的結果。

例外狀況

sourceselectornull

範例

下列程式代碼範例示範如何使用 SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>) 在數位上執行一對多投影,並使用每個外部專案的索引。

C#
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

備註

這個方法是使用延後執行來實作。 立即傳回值是一個物件,會儲存執行動作所需的所有資訊。 除非直接呼叫其 GetEnumerator 方法或在 C# 或 foreachFor Each Visual Basic 中使用 來列舉對象,否則不會執行這個方法所代表的查詢。

方法 SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>) 會列舉輸入序列、使用轉換函式將每個元素對應至 IEnumerable<T>,然後列舉併產生每個這類 IEnumerable<T> 對象的元素。 也就是說,會針對的每個專案 source叫用 , selector 並傳回一連串的值。 SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>) 然後將這個二維集合的集合壓平合併成一維 IEnumerable<T> ,並傳回它。 例如,如果查詢使用 SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>) 來取得資料庫中每個客戶) 類型的 Order 訂單 (,則結果的類型 IEnumerable<Order> 為 C# 或 IEnumerable(Of Order) Visual Basic。 如果查詢改用 Select 來取得訂單,則不會合併訂單集合的集合,而且結果的類型 IEnumerable<List<Order>> 為 C# 或 IEnumerable(Of List(Of Order)) Visual Basic。

要處理的第一個自變數 selector ,表示要處理的元素。 表示來源序列中該專案之以零起始的索引的第二個自變數 selector 。 例如,如果專案處於已知順序,而且您想要在特定索引處使用元素執行某些動作,這非常有用。 如果您想要擷取一或多個專案的索引,它也很有用。

適用於

.NET 9 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0