C# Collection was modified;enumeration operation may not execute
一、问题描述
在做 数组、列表、集合遍历时,可能我们会遇见这个问题。Collection was modified;enumeration operation may not execute ,翻译的中文意思:集合已修改;枚举操作可能无法执行。
二、解决方案
就是在遍历时,不要改变正在遍历的集合即可,您可以先遍历完在对其进行操作。
三、案例
出现问题前的代码如下,就是我在遍历 items 的同时,又往 items 中 add 数据。
- public async Task<ListResultDto<RecordBookListDto>> GetFlatRecordBookItems()
- {
- var query = _recordBookRepository
- .GetAll();
- var entities = await query.ToListAsync();
- var items = new List<RecordBookListDto>();
- foreach (var entity in entities)
- {
- var dto = entity.MapTo<RecordBookListDto>();
- items.Add(dto);
- }
- //todo 获取测点编号
- foreach (var item in items)
- {
- if (!string.IsNullOrEmpty(item.DataId))
- {
- String[] array = item.DataId.Replace("[", "").Replace("]", "").Replace("\"", "").Split(',');
- foreach (var ar in array)
- {
- var ins = _instrumentGroupRepository.Get(Guid.Parse(ar));
- var l = new RecordBookListDto();
- l.Id = Guid.Parse(ar);
- l.ParentId = item.Id.ToString();
- l.Name = ins.No;
- items.Add(l);
- }
- }
- }
- var listDto = new ListResultDto<RecordBookListDto>(items);
- return listDto;
- }
修改完成后的代码:
- public async Task<ListResultDto<RecordBookListDto>> GetFlatRecordBookItems()
- {
- var query = _recordBookRepository
- .GetAll();
- var entities = await query.ToListAsync();
- var items = new List<RecordBookListDto>();
- foreach (var entity in entities)
- {
- var dto = entity.MapTo<RecordBookListDto>();
- items.Add(dto);
- }
- List<RecordBookListDto> newItems = new List<RecordBookListDto>();
- //todo 获取测点编号
- foreach (var item in items)
- {
- if (!string.IsNullOrEmpty(item.DataId))
- {
- String[] array = item.DataId.Replace("[", "").Replace("]", "").Replace("\"", "").Split(',');
- foreach (var ar in array)
- {
- var ins = _instrumentGroupRepository.Get(Guid.Parse(ar));
- var l = new RecordBookListDto();
- l.Id = Guid.Parse(ar);
- l.ParentId = item.Id.ToString();
- l.Name = ins.No;
- newItems.Add(l);
- }
- }
- }
- foreach (var item in newItems)
- {
- items.Add(item);
- }
- var listDto = new ListResultDto<RecordBookListDto>(items);
- return listDto;
C# Collection was modified;enumeration operation may not execute的更多相关文章
- List使用Foreach 修改集合时,会报错的解决方案 (Error: Collection was modified; enumeration operation may not execute. ) - 摘自网络
当用foreach遍历Collection时,如果对Collection有Add或者Remove操作时,会发生以下运行时错误: "Collection was modified; enume ...
- 解决Collection was modified; enumeration operation may not execute异常
今天在使用foreach循环遍历list集合时,出现Collection was modified; enumeration operation may not execute.这个错误,查了半天才发 ...
- Collection was modified; enumeration operation may not execute.的异常处理
Collection was modified; enumeration operation may not execute.的异常处理 在运行程序时遇到这样一段异常,仔细检查后发现是使用Foreac ...
- Error: Collection was modified; enumeration operation may not execute.
http://blog.csdn.net/ffeiffei/article/details/6131254
- Collection was modified; enumeration operation may not execute Dictionary 集合已修改;可能无法执行枚举操作
public void ForeachDic() { Dictionary dic = new Dictionary(); dic.Add("1", 10); dic.Add(&q ...
- 关于struts2的checkboxlist、select等标签发生could not be resolved as a collection/array/map/enumeration/iterator type异常的记录
1 刚进入该界面的时候发生错误,原因是 list="roles"中的这个集合是空的,导致错误 解决办法很简单,不能让list为空 2 刚进入该界面的时候list是有数据的,当点击提 ...
- 做WP程序时遇到的一些问题及解决方法
问题1:Type 'JDBYSJ.Data.NewsChannel' cannot be serialized. Consider marking it with the DataContractAt ...
- C# 使用Linq递归查询数据库遇到的问题及解决方法
User表通常是我们在写"XX管理系统"项目时必须要用到的,有的情况下人员的分类属于树形结构,就是除了最高层和最低层,中间层都有相对的父和子,设计数据库的时候,我们通常会加一个pa ...
- C#在foreach循环中修改字典等集合出错的处理
C#在foreach循环中修改字典等集合出错:System.InvalidOperationException: Collection was modified; enumeration operat ...
随机推荐
- HDU 4714 Tree2cycle:贪心
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4714 题意: 给你一棵树,添加和删除一条边的代价都是1.问你将这棵树变成一个环的最小代价. 题解: 贪 ...
- HDU 4336 Card Collector:期望dp + 状压
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4336 题意: 一共有n种卡片.每买一袋零食,有可能赠送一张卡片,也可能没有. 每一种卡片赠送的概率为p ...
- MVC中ajax调用Controller的方法
1. ajax代码: $.ajax({ async: false, cache: false, type: 'POST', contentType: "application/json&qu ...
- 分享知识-快乐自己:Shiro 退出登陆清空缓存实现
shiro是一个被广泛使用的安全层框架,通过xml配置方式与spring无缝对接,用户的登陆/退出/权限控制/Cookie等管理系统基础功能交给shiro来管理. 一般,在JavaWEB管理平台系统时 ...
- python中的排序函数
1.sort() list类型有一个自带的排序函数sort() list.sort(cmp=None, key=None, reverse=False) 参数说明: (1) cmp参数 cmp接受一 ...
- stl_queue.h
stl_queue.h // Filename: stl_queue.h // Comment By: 凝霜 // E-mail: mdl2009@vip.qq.com // Blog: http:/ ...
- Spring MVC表单提交
实际应用中,列表中的单条记录的修改,可能需要传很多对象参数到后台服务器,Spring MVC表单标签<form:> 提供了一种简洁的提交方式. <form id="form ...
- java多线程编程核心技术——第三章总结
第一节等待/通知机制 1.1不使用等待/通知机制实现线程间的通讯 1.2什么是等待/通知机制 1.3等待/通知机制的实现 1.4方法wait()锁释放与notify()锁不释放 1.5当interru ...
- sysbench安装、使用
二.编译安装 编译非常简单,可参考 README 文档,简单步骤如下: cd/tmp/sysbench-0.4.12-1.1./autogen.sh./configure --with-mysql ...
- influxdb api
https://influxdb-python.readthedocs.io/en/latest/examples.html