https://www.jetbrains.com/help/resharper/2016.1/PossibleMultipleEnumeration.html

Consider the following code snippet:

IEnumerable<string> names = GetNames();
foreach (var name in names)
Console.WriteLine("Found " + name);
var allNames = new StringBuilder();
foreach (var name in names)
allNames.Append(name + " ");

Assuming that GetNames() returns an IEnumerable<string>, we are, effectively, doing extra work by enumerating this collection twice in the twoforeach statements.

If GetNames() results in a database query, you end up doing that query twice, while both times getting the same data.

This kind of problem can be easily fixed – simply force the enumeration at the point of variable initialization by converting the sequence to an array or a list, e.g.:

IEnumerable<string> names = GetNames().ToList();

The rest of your code can stay the same, because both array and list types implement the IEnumerable interface.

问题:如果GetNames是一个sql语句操作的话,2次的foreach会导致2次的sql查询

如果转为List,那么第二次遍历的时候,直接用第一次转换是List,就不会有2次的sql查询

最新文章

  1. iOS UITableView 引起的崩溃问题
  2. ASP.NET Web API 跨域访问
  3. 使用MSCOMM发送任意文件,还有一些注意事项
  4. hbase与Hadoop
  5. Serializable 序列化为文件
  6. delphi7调用java写的webservice,在调用的时候弹出“wssecurityhandler:request does not contain required security header”
  7. Linux中处理需要传输的IP报文流程
  8. SendMail如何签名
  9. javaScript 比较数字大小
  10. Debian 8 下安装持续集成的工具Jenkins
  11. JAVA下JSON的类型输出及使用
  12. MySQL EXTRACT() 函数
  13. C语言之linux内核实现位数高低位互换
  14. VMWAR-workstatuon
  15. E: 无法打开锁文件 /var/lib/dpkg/lock-frontend - open (13: 权限不够)E: 无法获取 dpkg 前端锁 (/var/lib/dpkg/lock-frontend),请查看您是否正以 root 用户运行?
  16. day13: 迭代器和生成器
  17. 苹果电脑thunderbolt连接两台电脑启动方法
  18. 界面编程之QT窗口系统20180726
  19. zsh与oh-my-zsh
  20. shell 字符串提取数字

热门文章

  1. 树莓派-解决apt-get upgrade速度慢的方法[更换阿里云源]
  2. Nginx作为负载均衡服务
  3. RFC1867 HTTP file upload
  4. MAVEN - 生命周期(1)
  5. 多种效果进度指示层效果iOS源码项目
  6. python2打印list中文内容防乱码
  7. ANE打包
  8. day37-3 异常处理
  9. 在Unity中json文件的解析方式
  10. Git创建本地分支并关联远程分支(二)