咨询区

  • Tarik

我已经 google 搜索了 SelectSelectMany 之间的区别,但我并没有找到合适的答案,我现在急切的需要知道在 Linq to SQL 时两者的区别而不是给我用Array展示...

能否有人帮忙提供 Linq To SQL 的例子吗?

回答区

  • Mike Two

SelectMany 它是对 列表中的列表 进行扁平化查询,比如下面的例子。


public class PhoneNumber
{public string Number { get; set; }
}public class Person
{public IEnumerable<PhoneNumber> PhoneNumbers { get; set; }public string Name { get; set; }
}IEnumerable<Person> people = new List<Person>();// Select gets a list of lists of phone numbers
IEnumerable<IEnumerable<PhoneNumber>> phoneLists = people.Select(p => p.PhoneNumbers);// SelectMany flattens it to just a list of phone numbers.
IEnumerable<PhoneNumber> phoneNumbers = people.SelectMany(p => p.PhoneNumbers);// And to include data from the parent in the result:
// pass an expression to the second parameter (resultSelector) in the overload:
var directory = people.SelectMany(p => p.PhoneNumbers,(parent, child) => new { parent.Name, child.Number });

  • Sriwantha Attanayake

SelectMany 类似 Sql 中的 cross join ,也就是所谓的笛卡尔积,比如下面的例子。


Set A={a,b,c}
Set B={x,y}

SelectMany 之后会得到如下结果。


{ (x,a) , (x,b) , (x,c) , (y,a) , (y,b) , (y,c) }

可以看出,上面就是罗列了 SetA 和 SetB 的所有组合,转换成 linq 的话可以这么写。


List<string> animals = new List<string>() { "cat", "dog", "donkey" };
List<int> number = new List<int>() { 10, 20 };var mix = number.SelectMany(num => animals, (n, a) => new { n, a });

输出结果如下:


{(10,cat), (10,dog), (10,donkey), (20,cat), (20,dog), (20,donkey)}

  • AlejandroR


var players = db.SoccerTeams.Where(c => c.Country == "Spain").SelectMany(c => c.players);foreach(var player in players)
{Console.WriteLine(player.LastName);
}
  1. De Gea

  2. Alba

  3. Costa

  4. Villa

  5. Busquets

点评区

很多时候,我发现越解释概念越说不清楚,最后说着说着就把 理科 变成了 文科,把逻辑变成了硬记????????????,我觉得这时候啥也不要说,直接看源码反而让人更清楚是咋回事。。。

  • Select 的底层逻辑


// System.Linq.Enumerable
using System.Collections.Generic;private static IEnumerable<TResult> SelectIterator<TSource, TResult>(IEnumerable<TSource> source, Func<TSource, int, TResult> selector)
{int index = -1;foreach (TSource item in source){index = checked(index + 1);yield return selector(item, index);}
}
  • SelectMany 的底层逻辑


// System.Linq.Enumerable
using System.Collections.Generic;private static IEnumerable<TResult> SelectManyIterator<TSource, TCollection, TResult>(IEnumerable<TSource> source, Func<TSource, IEnumerable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> resultSelector)
{foreach (TSource element in source){foreach (TCollection item in collectionSelector(element)){yield return resultSelector(element, item);}}
}

不知道你是否 豁然开朗, 不明白的话,快用 ILSpy 去挖掘 Enumerable 吧!

原文链接:https://stackoverflow.com/questions/958949/difference-between-select-and-selectmany

NET问答:Select 和 SelectMany 的区别相关推荐

  1. Select()和SelectMany()的区别

    Select与SelectMany的区别 Select() 和 SelectMany() 的工作都是依据源值生成一个或多个结果值. Select() 为每个源值生成一个结果值.因此,总体结果是一个与源 ...

  2. Windows Phone 7 IEnumerableT.Select和SelectMany的区别

    IEnumerable<T>在Windows Phone 7的程序上很常用,它允许开发人员定义foreach语句功能的实现并支持非泛型方法的简单迭代,下面主要分析一下 IEnumerabl ...

  3. IEnumerable.Select和SelectMany的区别

    例子(一个人可以有多个手机) public class People{public string Name { get; set; }public List<Phone> Phone { ...

  4. Select和SelectMany之间的区别

    我一直在搜索Select和SelectMany之间的区别,但我一直找不到合适的答案. 我需要学习使用LINQ To SQL的区别,但我发现的只是标准数组示例. 有人可以提供LINQ To SQL示例吗 ...

  5. linq里的select和selectmany操作 投影运算

    原文地址:https://msdn.microsoft.com/zh-cn/library/bb546168.aspx#Mtps_DropDownFilterText 投影运算 其他版本 投影是指将对 ...

  6. spark dataframe的select和selectexpr的区别

    对比: spark dataframe的select和selectexpr的区别 select是把要遍历的集合ienumerable逐一遍历,每次返回一个t,合并之后直接返回一个ienumerable ...

  7. select 和epoll模型区别

    1.select 和epoll模型区别 1.1.网络IO模型概述 通常来说,网络IO可以抽象成用户态和内核态之间的数据交换.一次网络数据读取操作(read),可以拆分成两个步骤:1)网卡驱动等待数据准 ...

  8. mysql select count() count(1)_select count()和select count(1)的区别和执行方式讲解

    select count()和select count(1)的区别和执行方式讲解 发布时间:2020-09-06 13:26:14 来源:脚本之家 阅读:227 作者:CODETC 在SQL Serv ...

  9. select count(*) 和 select count(1) 以及 select count(column) 的区别

    考试,目的在于让自己明白,自己天天写的都是垃圾 select count(*) 和 select count(1) 以及 select count(column) 的区别 1.如果表沒有主键, 那么c ...

最新文章

  1. mysql的安装和启动_mysql安装和启动
  2. 利用nvm管理Node的版本
  3. WebSocket客户端断开连接后,服务器端的处理机制
  4. java类的修改三个方面_Java 编程的动态性,第 6 部分: 利用 Javassist 进行面向方面的更改--转载...
  5. dj鲜生-32-用户中心-收货地址
  6. Spark源码系列(四)图解作业生命周期
  7. django 1.8 官方文档翻译: 6-6-4 部署静态文件
  8. Lucene.Net 2.3.1开发介绍 —— 一、接触Lucene.Net
  9. 网络语言维c是什么意思,我不要你觉得,我要我觉得!19年网络流行词是这些!...
  10. 【期末复习】计算机算法设计与分析
  11. Axure RP Extension for Chrome最新版查看RP原型
  12. VMware vmdk文件打开方法
  13. Java 扫描微信公众号二维码,关注并自动登录网站
  14. Sql中TO_DAYS,DATE_SUB等时间函数介绍
  15. 分布式系统设计权衡之CAP(一致性,可用性,分区容错性)
  16. mysql数据库从入门到高级
  17. MASC: Multi-scale Affinity with Sparse Convolution for 3D Instance Segmentation
  18. sqli-labs11-22关闯关心得与思路
  19. Launcher3去掉抽屉模式
  20. Android系统proc下查看cpuinfo的参数信息

热门文章

  1. 程序异常异常代码: 0xc0000005_Java基础:看完这篇你还怕碰到异常吗?
  2. Linux下查看文件内容的ASCII码以检查内容的编码一致
  3. [转]table中设置tr行间距
  4. Java字节码方法表与属性表深度剖析
  5. 【codevs1230】元素查找
  6. 移动web开发适配rem
  7. ChartCtrl源码剖析之——CChartAxis类
  8. windbg工具安装配置及dump抓取
  9. 查看端口被占用的进程号然后结束进程(解决端口被进程占用的问题)
  10. 退出Activity(转)