Resharper报“Possible multiple enumeration of IEnumerable”
问题描述:在IEnumerable使用时显示警告

分析:如果对IEnumerable多次读取操作,会有因数据源改变导致前后两次枚举项不固定的风险,最突出例子是读取数据库的时候,第二次foreach时恰好数据源发生了改变,那么读取出来的数据和第一次就不一致了。

查看测试代码
几乎所有返回类型为 IEnumerable<T> 或 IOrderedEnumerable<TElement> 的标准查询运算符都以延迟方式执行。如下表我们可以看到where时,返回的IEnumerable是延迟加载的。
|
标准查询运算符 |
Return Type |
立即执行 |
延迟流式执行 |
延迟非流式执行 |
|
TSource |
√ |
|||
|
√ |
||||
|
√ |
||||
|
√ |
||||
|
单个数值 |
√ |
|||
|
√ |
||||
|
√ |
||||
|
√ |
||||
|
√ |
||||
|
√ |
||||
|
√ |
||||
|
TSource |
√ |
|||
|
TSource |
√ |
|||
|
√ |
||||
|
√ |
√ |
|||
|
TSource |
√ |
|||
|
TSource |
√ |
|||
|
√ |
||||
|
√ |
√ |
|||
|
√ |
√ |
|||
|
√ |
√ |
|||
|
TSource |
√ |
|||
|
TSource |
√ |
|||
|
√ |
||||
|
单个数值、TSource 或 TResult |
√ |
|||
|
单个数值、TSource 或 TResult |
√ |
|||
|
√ |
||||
|
√ |
||||
|
√ |
||||
|
√ |
||||
|
√ |
||||
|
√ |
||||
|
√ |
||||
|
√ |
||||
|
√ |
||||
|
TSource |
√ |
|||
|
TSource |
√ |
|||
|
√ |
||||
|
√ |
||||
|
单个数值 |
√ |
|||
|
√ |
||||
|
√ |
||||
|
√ |
||||
|
√ |
||||
|
TSource 数组 |
√ |
|||
|
√ |
||||
|
√ |
||||
|
√ |
||||
|
√ |
||||
|
√ |
解决方案:
多次使用IEnumerable时,最好转换为List或者Array
测试代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using ConsoleApplication2.EF; namespace ConsoleApplication2
{
class Program_IEnumerable
{
static void Main(string[] args)
{
// 异步访问数据库
Task.Run(() =>
{
while (true)
{
reloadDb();
}
}); // 使用死循环不停的读取数据
int count = ;
while (true)
{
Console.WriteLine("第{0}读取", count);
IEnumerable<string> names = getNames(); var allNames = new StringBuilder();
foreach (var name in names)
allNames.Append(name + ",");
Thread.Sleep(); var allNames2 = new StringBuilder();
foreach (var name in names)
allNames2.Append(name + ",");
if (allNames2 != allNames)
Console.WriteLine("数据源发生了改变");
count++; Thread.Sleep();
} Console.ReadKey();
} static void reloadDb()
{
using (var infosEntities = new TestEntities())
{
infosEntities.Student.Add(new Student
{
EnrollmentDate = DateTime.Now,
FirstMidName = "han",
LastName = "zhu"
});
infosEntities.SaveChanges();
}
Thread.Sleep(); using (var infosEntities = new TestEntities())
{
var entity = infosEntities.Student.FirstOrDefault(a => a.FirstMidName == "han");
if (entity != null)
{
infosEntities.Student.Remove(entity);
infosEntities.SaveChanges();
}
}
Thread.Sleep();
} static IEnumerable<string> getNames()
{
var infosEntities = new TestEntities();
return infosEntities.Student.Select(a => a.FirstMidName + " " + a.LastName);
} } }
参考资料:
Resharper官方对于这个警告的描述:
https://www.jetbrains.com/help/resharper/PossibleMultipleEnumeration.html
MSDN的解释:
https://msdn.microsoft.com/zh-cn/library/vs/alm/bb882641(v=vs.90)/css
Resharper报“Possible multiple enumeration of IEnumerable”的更多相关文章
- Possible multiple enumeration of IEnumerable
https://www.jetbrains.com/help/resharper/2016.1/PossibleMultipleEnumeration.html Consider the follow ...
- [报错]Fast enumeration variables cannot be modified in ARC by default; declare the variable __strong to allow this
今天写了下面的快速枚举for循环代码,从按钮数组subButtons中取出button,然后修改button的样式,在添加到view中 for (UIButton *button in subButt ...
- web.xml里报错:Multiple annotations found at this line:
在web.xml 中添加错误页面配置,出现了这个报错 具体情况是这样的: 错误信息: Multiple annotations found at this line: - cvc-complex-ty ...
- hibernate和mybatis出现配置文件xml的文件报错Multiple annotations found at this line(转)
hibernate中的xml配置文件Multiple annotations found at this line,出现这个红叉报错,直接是把 <?xml version="1.0&q ...
- ASP.NET MVC报错: Multiple types were found that match the controller named
当使用ASP.NET MVC的Area功能时,出现了这样的错误: Multiple types were found that match the controller named 'Home'. T ...
- 关于MyEclipse不停报错multiple problems have occurred 或者是内存不足 的解决办法
这是因为 worksapace与svn代码不一样,要更新! 一更新就好了,困扰死我了,卧槽,搞了2个小时,难怪svn一提交就卡死人,原来还就是svn的问题,更新一下就行.
- 异常-----web.xml文件报错 Multiple annotations found at this line: - cvc-complex-type.2.4.b: The content of element 'welcome-file-list' is not complete. One of '{"http://java.sun.c
1,检查抬头是不是有问题. <?xml version="1.0" encoding="UTF-8"?><web-app version=&q ...
- eclipse导入项目报错multiple annotations found at this line
eclipsewindow-->preference-->Valdation-->将Manual和Build下复选框全部取消选择
- as报错 Multiple root tags Unexpected tokens 这个都是编译器识别问题
从网上复制了个代码,直接复制上,结果一篇红线提示Unexpected tokens 通过去掉空格,还是无法根治,别的地方复制的就没有问题. 通过查看复制的网页源码 可以看到里边<> 这个符 ...
随机推荐
- [shell] 循环判断输入值
做个记录 until [[ $flag == "yes" || $flag == "exit" ]] do read -p "请确认统一/合服前后数据 ...
- SVN服务器搭建及客户端配置
为什么要使用SVN? 在程序的编写过程中,每个程序员都会负责开发一个或多个模块,且开发中会生成很多不同的版本, 这就需要程序员有效的管理代码,在需要的时候可以迅速,准确取出相应的版本. Subvers ...
- LCA最近公共祖先(Tarjan离线算法)
这篇博客对Tarjan算法的原理和过程模拟的很详细. 转载大佬的博客https://www.cnblogs.com/JVxie/p/4854719.html 第二次更新,之前转载的博客虽然胜在详细,但 ...
- “Hello World!”团队召开的第六次会议
团队“Hello World!”团队召开的第六次会议. 博客内容: 一.会议时间 二.会议地点 三.会议成员 四.会议内容 五.Todo List 六.会议照片 七.燃尽图 一.会议时间 2017年1 ...
- 20172329 2018-2019《Java软件结构与数据结构》第一周学习总结
2018-2019-20172329 <Java软件结构与数据结构>第一周学习总结 在这学期就已经大二了,也已经步入了学习专业课的核心时间,在这个阶段,我们应该了解自己的学习情况,针对自己 ...
- HDU 1754 I Hate It 线段树(单点更新,成段查询)
题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=1754 题解: 单点更新,成段查询. 代码: #include<iostream> ...
- 周总结<1>
由于都不清楚周总结的格式,所以就没有写了.不过,上次听了老师的课,觉得应该要好好写写了,至少今后可以明白自己有做过什么事情,至少不会觉得自己在各个方面没有收获.不过,可能没有按照格式来写.希望老师体谅 ...
- 基于图形学混色问题OpenGl的收获
void myDisplay(void) {glClearColor(0.0f,0.0f,0.0f,1.0f); glClear(GL_COLOR_BUFFER_BIT); glEnable(GL_B ...
- p2 入门
心里一片空白,要弄个p2的demo出来... 先了解下p2的概念吧 P2只是一个算法库,以刚体为对象模型,模拟并输出物理碰撞.运动结果.这个过程通过持续调用world中的step()方法来实现 p2的 ...
- 更新user的方法
from django.contrib.auth.admin import UserAdmin from django.contrib.auth.forms import UserChangeForm ...