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 ...
随机推荐
- 黑客常用dos命令
http://blog.csdn.net/CSDN___LYY/article/details/77802438
- AI:IPPR的数学表示-CNN方法
前言: 随着超量类别PR和高精度的需求,人工特征方法局限性凸显出来,固定的特征hash压缩映射因其压缩损失.表现为特定的特征hash方法,在海量的同类数据集上近邻特性变差,而在不同类别的数据上面隔离性 ...
- POJ_1061_扩展欧几里德
青蛙的约会 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 107027 Accepted: 21321 Descript ...
- POJ_2594_最小路径覆盖
Treasure Exploration Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 8085 Accepted: 3 ...
- 新浪某个tab 页模仿
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- The remote certificate is invalid according to the validation procedure 远程证书验证无效
The remote certificate is invalid according to the validation procedure 根据验证过程中远程证书无效 I'm calling ...
- Js 删除前弹出确认框
<td align="center" valign="middle" class="black3"> <c:if test ...
- sqlserver系统表使用
SELECT s.table_catalog as 数据库名, o.name as 表名, c.name as 列名FROM INFORMATION_SCHEMA.TABLES s,--库 sys.o ...
- eas之载入编辑界面时设置明细默认值createNewDate()
protected com.kingdee.bos.dao.IObjectValue.createNewDate(){ //new一个对象 PurOrderInfo info=new Pu ...
- js对比for、forEach、map遍历数组速度
function a() { var arr = new Array(1000000); for(var i = 0; i < arr.length;i ++) { arr[i] = i; } ...