Queryable.SelectMany 方法

定義

將序列的每個項目都投影成 IEnumerable<T>,並將產生的序列合併成一個型別為 IQueryable<T> 的序列。

多載

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

將序列的每個項目投影成 IEnumerable<T>,並在其中的每個項目上叫用結果選取器函式。 每個中繼序列產生的值都會合併成單一的一維序列,然後再傳回。

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

將序列的每個項目都投影成 IEnumerable<T>,以合併產生該項目之來源項目的索引。 接著對各中繼序列的每個項目叫用結果選取器函式,然後將產生的值合併成單一的一維序列並傳回。

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

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

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

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

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

來源:
Queryable.cs
來源:
Queryable.cs
來源:
Queryable.cs

將序列的每個項目投影成 IEnumerable<T>,並在其中的每個項目上叫用結果選取器函式。 每個中繼序列產生的值都會合併成單一的一維序列,然後再傳回。

C#
public static System.Linq.IQueryable<TResult> SelectMany<TSource,TCollection,TResult> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,System.Collections.Generic.IEnumerable<TCollection>>> collectionSelector, System.Linq.Expressions.Expression<Func<TSource,TCollection,TResult>> resultSelector);

類型參數

TSource

source 項目的類型。

TCollection

collectionSelector 表示之函式所收集之中繼項目的型別。

TResult

產生的序列之項目型別。

參數

source
IQueryable<TSource>

要投影的值序列。

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

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

resultSelector
Expression<Func<TSource,TCollection,TResult>>

要套用到各中繼序列之各個項目的投影函式。

傳回

IQueryable<TResult>

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

例外狀況

sourcecollectionSelectorresultSelectornull

範例

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

C#
class PetOwner
{
    public string Name { get; set; }
    public List<Pet> Pets { get; set; }
}

class Pet
{
    public string Name { get; set; }
    public string Breed { get; set; }
}

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

    // This query demonstrates how to obtain a sequence of
    // the names of all the pets whose breed is "Collie", while
    // keeping an association with the owner that owns the pet.
    var query =
        petOwners.AsQueryable()
        // Create a sequence of ALL the Pet objects. Then
        // project an anonymous type that consists of each
        // Pet in the new sequence and the PetOwner object
        // from the initial array that corresponds to that pet.
       .SelectMany(owner => owner.Pets,
                   (owner, pet) => new { owner, pet })
        // Filter the sequence of anonymous types to only
        // keep pets whose breed is "Collie".
        .Where(ownerAndPet => ownerAndPet.pet.Breed == "Collie")
        // Project an anonymous type that consists
        // of the pet owner's name and the pet's name.
        .Select(ownerAndPet => new
        {
            Owner = ownerAndPet.owner.Name,
            Pet = ownerAndPet.pet.Name
        });

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

/* This code produces the following output:

    { Owner = Ashkenazi, Pet = Walker }
    { Owner = Price, Pet = Diesel }
    { Owner = Hines, Pet = Dusty }
*/

備註

這個方法至少有一個類型的參數,其類型 Expression<TDelegate> 自變數為其中一個型別 Func<T,TResult> 。 針對這些參數,您可以傳入 Lambda 運算式,並將它編譯為 Expression<TDelegate>

方法 SelectMany<TSource,TCollection,TResult>(IQueryable<TSource>, Expression<Func<TSource, IEnumerable<TCollection>>>, Expression<Func<TSource,TCollection, TResult>>) 會產生 , MethodCallExpression 表示呼叫 SelectMany<TSource,TCollection,TResult>(IQueryable<TSource>, Expression<Func<TSource, IEnumerable<TCollection>>>, Expression<Func<TSource,TCollection, TResult>>) 本身為建構的泛型方法。 然後,它會將 傳遞給 MethodCallExpressionCreateQuery(Expression) 參數之 屬性所Provider表示的方法IQueryProvidersource

執行表示呼叫 SelectMany<TSource,TCollection,TResult>(IQueryable<TSource>, Expression<Func<TSource, IEnumerable<TCollection>>>, Expression<Func<TSource,TCollection, TResult>>) 的表達式樹狀結構所產生的查詢行為,取決於參數類型的實作 source 。 預期的行為是在 的每個元素source上叫collectionSelector用,以將它投影成可列舉的形式。 然後,在每個中繼序列中的每個專案上叫用 所 resultSelector 代表的函式。 產生的值會串連成單一一維的一維序列。

適用於

.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 2.0, 2.1
UWP 10.0

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

來源:
Queryable.cs
來源:
Queryable.cs
來源:
Queryable.cs

將序列的每個項目都投影成 IEnumerable<T>,以合併產生該項目之來源項目的索引。 接著對各中繼序列的每個項目叫用結果選取器函式,然後將產生的值合併成單一的一維序列並傳回。

C#
public static System.Linq.IQueryable<TResult> SelectMany<TSource,TCollection,TResult> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,int,System.Collections.Generic.IEnumerable<TCollection>>> collectionSelector, System.Linq.Expressions.Expression<Func<TSource,TCollection,TResult>> resultSelector);

類型參數

TSource

source 項目的類型。

TCollection

collectionSelector 表示之函式所收集之中繼項目的型別。

TResult

產生的序列之項目型別。

參數

source
IQueryable<TSource>

要投影的值序列。

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

要套用到輸入序列每個項目的投影函式;此函式的第二個參數代表來源項目的索引。

resultSelector
Expression<Func<TSource,TCollection,TResult>>

要套用到各中繼序列之各個項目的投影函式。

傳回

IQueryable<TResult>

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

例外狀況

sourcecollectionSelectorresultSelectornull

備註

這個方法至少有一個類型的參數,其類型 Expression<TDelegate> 自變數為其中一個型別 Func<T,TResult> 。 針對這些參數,您可以傳入 Lambda 運算式,並將它編譯為 Expression<TDelegate>

方法 SelectMany<TSource,TCollection,TResult>(IQueryable<TSource>, Expression<Func<TSource, Int32,IEnumerable<TCollection>>>, Expression<Func<TSource,TCollection, TResult>>) 會產生 , MethodCallExpression 表示呼叫 SelectMany<TSource,TCollection,TResult>(IQueryable<TSource>, Expression<Func<TSource, Int32,IEnumerable<TCollection>>>, Expression<Func<TSource,TCollection, TResult>>) 本身為建構的泛型方法。 然後,它會將 傳遞給 MethodCallExpressionCreateQuery(Expression) 參數之 屬性所Provider表示的方法IQueryProvidersource

執行表示呼叫 SelectMany<TSource,TCollection,TResult>(IQueryable<TSource>, Expression<Func<TSource, Int32,IEnumerable<TCollection>>>, Expression<Func<TSource,TCollection, TResult>>) 的表達式樹狀結構所產生的查詢行為,取決於參數類型的實作 source 。 預期的行為是在 的每個元素source上叫collectionSelector用,以將它投影成可列舉的形式。 每個可列舉的結果都包含來源元素的索引。 然後,在每個中繼序列中的每個專案上叫用 所 resultSelector 代表的函式。 產生的值會串連成單一一維的一維序列。

適用於

.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 2.0, 2.1
UWP 10.0

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

來源:
Queryable.cs
來源:
Queryable.cs
來源:
Queryable.cs

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

C#
public static System.Linq.IQueryable<TResult> SelectMany<TSource,TResult> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,System.Collections.Generic.IEnumerable<TResult>>> selector);

類型參數

TSource

source 項目的類型。

TResult

selector 表示之函式所傳回序列之項目的型別。

參數

source
IQueryable<TSource>

要投影的值序列。

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

要套用到每個項目的投影函式。

傳回

IQueryable<TResult>

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

例外狀況

sourceselectornull

範例

下列程式代碼範例示範如何使用 SelectMany<TSource,TResult>(IQueryable<TSource>, Expression<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.AsQueryable().SelectMany(petOwner => petOwner.Pets);

    Console.WriteLine("Using SelectMany():");

    // Only one foreach loop is required to iterate through the
    // results because 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.AsQueryable().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
*/

備註

這個方法至少有一個類型的參數,其類型 Expression<TDelegate> 自變數為其中一個型別 Func<T,TResult> 。 針對這些參數,您可以傳入 Lambda 運算式,並將它編譯為 Expression<TDelegate>

方法 SelectMany<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,IEnumerable<TResult>>>) 會產生 , MethodCallExpression 表示呼叫 SelectMany<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,IEnumerable<TResult>>>) 本身為建構的泛型方法。 然後,它會將 傳遞給 MethodCallExpressionCreateQuery(Expression) 參數之 屬性所Provider表示的方法IQueryProvidersource

執行表示呼叫 SelectMany<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,IEnumerable<TResult>>>) 的表達式樹狀結構所產生的查詢行為,取決於參數類型的實作 source 。 預期的行為是在 的每個元素source上叫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 2.0, 2.1
UWP 10.0

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

來源:
Queryable.cs
來源:
Queryable.cs
來源:
Queryable.cs

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

C#
public static System.Linq.IQueryable<TResult> SelectMany<TSource,TResult> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,int,System.Collections.Generic.IEnumerable<TResult>>> selector);

類型參數

TSource

source 項目的類型。

TResult

selector 表示之函式所傳回序列之項目的型別。

參數

source
IQueryable<TSource>

要投影的值序列。

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

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

傳回

IQueryable<TResult>

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

例外狀況

sourceselectornull

範例

下列程式代碼範例示範如何使用 SelectMany<TSource,TResult>(IQueryable<TSource>, Expression<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" } } };

    // For each PetOwner element in the source array,
    // project a sequence of strings where each string
    // consists of the index of the PetOwner element in the
    // source array and the name of each pet in PetOwner.Pets.
    IEnumerable<string> query =
        petOwners.AsQueryable()
        .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

備註

這個方法至少有一個類型的參數,其類型 Expression<TDelegate> 自變數為其中一個型別 Func<T,TResult> 。 針對這些參數,您可以傳入 Lambda 運算式,並將它編譯為 Expression<TDelegate>

方法 SelectMany<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,Int32,IEnumerable<TResult>>>) 會產生 , MethodCallExpression 表示呼叫 SelectMany<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,Int32,IEnumerable<TResult>>>) 本身為建構的泛型方法。 然後,它會將 傳遞給 MethodCallExpressionCreateQuery(Expression) 參數之 屬性所Provider表示的方法IQueryProvidersource

執行表示呼叫 SelectMany<TSource,TResult>(IQueryable<TSource>, Expression<Func<TSource,Int32,IEnumerable<TResult>>>) 的表達式樹狀結構所產生的查詢行為,取決於參數類型的實作 source 。 預期的行為是在 的每個元素source上叫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 2.0, 2.1
UWP 10.0