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 通过去掉空格,还是无法根治,别的地方复制的就没有问题. 通过查看复制的网页源码 可以看到里边<> 这个符 ...
随机推荐
- sqlserver-查阻塞
模拟阻塞: 打开两个窗口: 窗口一: BEGIN TRANSACTION--开始事务 --等待1分钟 WAITFOR DELAY '00:1'; 窗口二: 查询阻塞:(当前被阻塞的进程id,不 ...
- 404_NOTE_Foung_软工6
目录 NABCD分析引用 N(Need,需求): A(Approach,做法): B(Benefit,好处): C(Competitors,竞争): D(Delivery,交付): 初期 中期 个人贡 ...
- [codecademy]fonts in css
Great job! You learned how to style an important aspect of the user experience: fonts! Let's review ...
- Java中按值传递与按引用传递的区别
值传递:(形式参数类型是基本数据类型):方法调用时,实际参数把它的值传递给对应的形式参数,形式参数只是用实际参数的值初始化自己的存储单元内容,是两个不同的存储单元,所以方法执行中形式参数值的改变不影响 ...
- Tomcat配置 —— server.xml
Tomcat的核心组件是servlet容器. Tomcat各个组件之间的嵌套关系 server.xml配置如下: <Server port="8005" shutdown=& ...
- 七周七语言之使用prolog解决爱因斯坦斑马难题
如果你想获得更好的阅读体验,可以前往我在 github 上的博客进行阅读,http://lcomplete.github.io/blog/2013/06/28/sevenlang-prolog/. 目 ...
- jdbc 5.0
1.事务 事务将单个SQL语句或一组SQL语句视为一个逻辑单元,如果任何语句失败,整个事务将失败. jdbc的MySQL驱动程序中的事务默认是自动提交. 默认情况下,每个SQL语句在完成后都会提交到数 ...
- JDK1.8 之Lambda
Lambda 理解的了很久才有一点小感觉. 语法 lambda表达式的特点,它的语法如下面. parameter -> expression body 下面是一个lambda表达式的重要特征. ...
- 实现全站 HTTPS ,为什么国内网站总是那么slow&&low呀!
1 https://konklone.com/post/switch-to-https-now-for-free# https://theintercept.com/2014/11/20/non-pr ...
- 'phantomjs.exe' executable needs to be in PATH. (selenium PhantomJS python)
今天selenium PhantomJS python用了下,发现报错,提示我:'phantomjs.exe' executable needs to be in PATH. from seleniu ...