首页 > 其他分享 >Enumerable.SelectMany 方法

Enumerable.SelectMany 方法

时间:2024-04-14 19:56:24浏览次数:14  
标签:Pets Enumerable 元素 IEnumerable Func new 方法 SelectMany

Enumerable.SelectMany 方法

定义

命名空间:
System.Linq
程序集:
System.Linq.dll

将序列的每个元素投影到 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>)

将序列的每个元素投影到 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>,其元素是通过以下方法得到的:对 source 的每个元素调用一对多转换函数 collectionSelector,然后将这些序列元素中的每一个元素及其相应的源元素映射到一个结果元素。

例外

ArgumentNullException

sourcecollectionSelector 或 resultSelector 为 null

示例

下面的代码示例演示如何使用 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# For Each 或 foreach 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 对象之间存在双向关系,即,如果 类型的TCollection对象提供属性来检索TSource生成它的 对象,则不需要 的此重载SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>)TCollection。 相反,可以使用 SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>) 并通过 对象TCollection导航回 TSource 对象。

在查询表达式语法中,在初始子句转换为 调用 SelectMany后,每个from子句 (C#) 或 From 子句 (Visual Basic) 。

另请参阅

适用于

.NET 9 和其他版本
  
   
   
   
   

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

将序列的每个元素投影到 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>,其元素是通过以下方法得到的:对 source 的每个元素调用一对多转换函数 collectionSelector,然后将这些序列元素中的每一个元素及其相应的源元素映射到一个结果元素。

例外

ArgumentNullException

sourcecollectionSelector 或 resultSelector 为 null

注解

此方法通过使用延迟执行来实现。 即时返回值是一个对象,用于存储执行操作所需的所有信息。 在通过直接调用GetEnumerator其方法或在 C# For Each 或 foreach 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 对象之间存在双向关系,即,如果 类型的TCollection对象提供属性来检索TSource生成它的 对象,则不需要 的此重载SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>)TCollection。 相反,可以使用 SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>) 并通过 对象TCollection导航回 TSource 对象。

适用于

.NET 9 和其他版本
  
   
   
   
   

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

将序列的每个元素投影到 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>,其元素是对输入序列的每个元素调用一对多转换函数得到的结果。

例外

ArgumentNullException

source 或 selector 为 null

示例

下面的代码示例演示如何使用 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# For Each 或 foreach 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 和其他版本
  
   
   
   
   

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

将序列的每个元素投影到 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>,其元素是对输入序列的每个元素调用一对多转换函数得到的结果。

例外

ArgumentNullException

source 或 selector 为 null

示例

下面的代码示例演示如何使用 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# For Each 或 foreach 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 和其他版本
  
   
   
   
   
     在 GitHub 上与我们协作 可以在 GitHub 上找到此内容的源,还可以在其中创建和查看问题和拉取请求。 有关详细信息,请参阅参与者指南

.NET 反馈

.NET 是一个开放源代码项目。 选择一个链接以提供反馈:

 提出文档问题 提供产品反馈

本文内容

  1. 定义
  2. 重载
  3. SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>)
  4. SelectMany<TSource,TCollection,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TCollection>>, Func<TSource,TCollection,TResult>)
  5. SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,IEnumerable<TResult>>)
  6. SelectMany<TSource,TResult>(IEnumerable<TSource>, Func<TSource,Int32,IEnumerable<TResult>>)
中文 (简体) 你的隐私选择

标签:Pets,Enumerable,元素,IEnumerable,Func,new,方法,SelectMany
From: https://www.cnblogs.com/sexintercourse/p/18134580

相关文章

  • 实现自定义注解校验方法参数(AOP+自定义注解+自定义异常+全局异常捕获)
    一、实现目的在编写接口的时候,通常会先对参数进行一次校验,这样业务逻辑代码就略显冗杂,如果可以把校验参数的代码进行统一管理,在方法或者属性上直接添加注解就可以实现参数的校验,就可以提升代码编写的效率。二、实现原理通过自定义注解,注解在入参VO的属性上,设定需要满足的条件,然......
  • JavaScript判断图片是否已经加载完毕的方法汇总_javascript技巧
    JavaScript判断图片是否已经加载完毕的方法汇总_javascript技巧 在网上有很多关于判断图片是否已经加载完毕的文章,但是有的浏览器并不适合,下面小编给大家分享一些有关JavaScript判断图片是否已经加载完毕方法汇总,具体内容如下所示:一.onload事件通过监听图片的onload事件,可......
  • populateBean方法解析
    populateBean方法实现的功能autowired解析Autowired实例,code如下:packagecom.gientech.populateBean.annotation;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Controller;@ControllerpublicclassBook......
  • Promise 内置的方法有哪些
    在JavaScript中,Promise对象有几个内置的方法:Promise.resolve(value):返回一个解析过的Promise对象,该对象的状态和值分别由value决定。Promise.reject(reason):返回一个拒绝的Promise对象,该对象的状态和原因分别由reason决定。Promise.all(iterable):接收一个可迭代......
  • split(),slice(),splice(),join()使用方法
    比如一个例子:有一个字符串:pdf,jpg,png,jpge,doc,docx,xls,xlsx,ppt,pptx,rar,zip把它修改成以逗号连接的扩展名字符串,方法如下:varaccept="pdf,jpg,png,jpge,doc,docx,xls,xlsx,ppt,pptx,rar,zip";varnewStr="."+accept.toLowerCase().split(",").join(",.")......
  • Java中的matches()方法与find()方法区别
    正则表达式用于在字符串中进行模式匹配。在处理字符串时,经常需要使用matches()方法和find()方法来查找与正则表达式匹配的部分。虽然它们都用于匹配字符串,但有一些重要的区别。matches()方法matches()方法是String类中的一个方法,用于判断整个字符串是否与给定的正则表达......
  • 有限元方法[Matlab]-笔记
    <<MATLABCodesforFiniteElementAnalysis-SolidsandStructures(Ferreira)>>笔记chapter01matlabbasic略第二章:离散系统笔记、例题Matlab代码problem1.m%MATLABcodesforFiniteElementAnalysis%Problem1:3springsproblem%clearme......
  • 15.元表与元方法
    元表,即Lua中普通table的元数据表,而元方法则是元表中定义的普通表的默认行为。Lua中的每个普通table都可为其定义一个元表,用于扩展该普通table的行为功能。例如,对于table与数值相加的行为,Lua中是没有定义的,但用户可通过为其指定元表来扩展这种行为;再如,用户访问不存......
  • microsoft全球GlobalMLBuildingFootprints下载方法
    website:https://github.com/microsoft/GlobalMLBuildingFootprints?tab=readme-ov-filePython代码Start"""Thissnippetdemonstrateshowtoaccessandconvertthebuildingsdatafrom.csv.gztogeojsonforuseincommonGIStools.Youwillneedtoi......
  • 多通道的AXI仲裁方法V3
    https://www.cnblogs.com/VerweileDoch/p/18030653V2。读moduleAribe_state_rd#(parameterintegerM_AXI_ID_WIDTH=1,parameterintegerM_AXI_ADDR_WIDTH=32,parameterintege......