Possible multiple enumeration of IEnumerable
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查询
Possible multiple enumeration of IEnumerable的更多相关文章
- Resharper报“Possible multiple enumeration of IEnumerable”
问题描述:在IEnumerable使用时显示警告 分析:如果对IEnumerable多次读取操作,会有因数据源改变导致前后两次枚举项不固定的风险,最突出例子是读取数据库的时候,第二次foreach时恰 ...
- yield return的使用。。。
因为要取两个集合不同的元素,所以写了个拓展方法,用到了yield这个关键字,然后就学习了一波.先上代码 public static IEnumerable<T> NoRetainAll&l ...
- The main reborn ASP.NET MVC4.0: using CheckBoxListHelper and RadioBoxListHelper
The new Helpers folder in the project, to create the CheckBoxListHelper and RadioBoxListHelper class ...
- [No0000183]Parallel Programming with .NET-How PLINQ processes an IEnumerable<T> on multiple cores
As Ed Essey explained in Partitioning in PLINQ, partitioning is an important step in PLINQ execution ...
- IEnumerator和IEnumerable详解
IEnumerator和IEnumerable 从名字常来看,IEnumerator是枚举器的意思,IEnumerable是可枚举的意思. 了解了两个接口代表的含义后,接着看源码: IEnumerat ...
- JTAG 引脚自动识别 JTAG Finder, JTAG Pinout Tool, JTAG Pin Finder, JTAG pinout detector, JTAGULATOR, Easy-JTAG, JTAG Enumeration
JTAG Finder Figuring out the JTAG Pinouts on a Device is usually the most time-consuming and frustra ...
- Why there is two completely different version of Reverse for List and IEnumerable?
https://stackoverflow.com/questions/12390971/why-there-is-two-completely-different-version-of-revers ...
- 先说IEnumerable,我们每天用的foreach你真的懂它吗?
我们先思考几个问题: 为什么在foreach中不能修改item的值? 要实现foreach需要满足什么条件? 为什么Linq to Object中要返回IEnumerable? 接下来,先开始我们的正 ...
- 为IEnumerable<T>添加RemoveAll<IEnumerable<T>>扩展方法--高性能篇
最近写代码,遇到一个问题,微软基于List<T>自带的方法是public bool Remove(T item);,可是有时候我们可能会用到诸如RemoveAll<IEnumerab ...
随机推荐
- 复习java基础第一天
一:static static: 静态的. 1. 若需要一个类的多个对象共享一个变量,则该变量需要使用 static 修饰. 2. 因为 static 修饰的变量为类的所有的实例所共享,所以 ...
- jQuery插件的怎么写
对于jQuery之前一直用,也看到过别人写的插件,直到最近才想着学习怎么写自己的jQuery插件,今天看了网上的一些资料,发现其实很简单的. 先看一个简单的jQuery插件的例子 <script ...
- 【转】虚拟化(一):虚拟化及vmware产品介绍
由于公司最近在做虚拟化监控,因此就需要把虚拟化方面的知识给学习总结一下,对于虚拟化的概念,摘自百度百科,如下: 虚拟化,是指通过虚拟化技术将一台计算机虚拟为多台逻辑计算机.在一台计算机 ...
- BZOJ 1715: [Usaco2006 Dec]Wormholes 虫洞 DFS版SPFA判负环
Description John在他的农场中闲逛时发现了许多虫洞.虫洞可以看作一条十分奇特的有向边,并可以使你返回到过去的一个时刻(相对你进入虫洞之前).John的每个农场有M条小路(无向边)连接着N ...
- TestNg学习一
简介 TestNG是Java中的一个测试框架, 类似于JUnit 和NUnit, 功能都差不多, 只是功能更加强大,使用也更方便 Java中已经有一个JUnit的测试框架了. TestNG比JU ...
- vue小白学习笔记
<div id="div"> <h2>{{ key }}</h2> </div> new Vue ({ el : "#di ...
- SpringBoot快速创建HelloWorld项目
废话不多提,拿起键盘,打开 IDEA 就是一通骚操作. 打开 IDEA 后,首页选择 Create New Project,再接着按下图所示,快速搭建SpringBoot项目. 接下来将 Group ...
- DataInputStream 数据类型数据输入输出流
package IOliu; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileI ...
- 55.TF/IDF算法
主要知识点: TF/IDF算法介绍 查看es计算_source的过程及各词条的分数 查看一个document是如何被匹配到的 一.算法介绍 relevance score算法,简单来说 ...
- StringUtils.isNotBlank 和StringUtils.isNotEmpty 的区别
StringUtils.isNotBlank判断某字符串是否不为空且长度不为0且不由空白符(whitespace)构成下面是示例:StringUtils.isNotBlank(null) = fals ...