一、问题描述

  在做 数组、列表、集合遍历时,可能我们会遇见这个问题。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的更多相关文章

  1. List使用Foreach 修改集合时,会报错的解决方案 (Error: Collection was modified; enumeration operation may not execute. ) - 摘自网络

    当用foreach遍历Collection时,如果对Collection有Add或者Remove操作时,会发生以下运行时错误: "Collection was modified; enume ...

  2. 解决Collection was modified; enumeration operation may not execute异常

    今天在使用foreach循环遍历list集合时,出现Collection was modified; enumeration operation may not execute.这个错误,查了半天才发 ...

  3. Collection was modified; enumeration operation may not execute.的异常处理

    Collection was modified; enumeration operation may not execute.的异常处理 在运行程序时遇到这样一段异常,仔细检查后发现是使用Foreac ...

  4. Error: Collection was modified; enumeration operation may not execute.

    http://blog.csdn.net/ffeiffei/article/details/6131254

  5. Collection was modified; enumeration operation may not execute Dictionary 集合已修改;可能无法执行枚举操作

    public void ForeachDic() { Dictionary dic = new Dictionary(); dic.Add("1", 10); dic.Add(&q ...

  6. 关于struts2的checkboxlist、select等标签发生could not be resolved as a collection/array/map/enumeration/iterator type异常的记录

    1 刚进入该界面的时候发生错误,原因是 list="roles"中的这个集合是空的,导致错误 解决办法很简单,不能让list为空 2 刚进入该界面的时候list是有数据的,当点击提 ...

  7. 做WP程序时遇到的一些问题及解决方法

    问题1:Type 'JDBYSJ.Data.NewsChannel' cannot be serialized. Consider marking it with the DataContractAt ...

  8. C# 使用Linq递归查询数据库遇到的问题及解决方法

    User表通常是我们在写"XX管理系统"项目时必须要用到的,有的情况下人员的分类属于树形结构,就是除了最高层和最低层,中间层都有相对的父和子,设计数据库的时候,我们通常会加一个pa ...

  9. C#在foreach循环中修改字典等集合出错的处理

    C#在foreach循环中修改字典等集合出错:System.InvalidOperationException: Collection was modified; enumeration operat ...

随机推荐

  1. Java -- 键盘输入 Scanner, BufferedReader。 系统相关System,Runtime。随机数 Randrom。日期操作Calendar

    1. Scanner 一个基于正则表达式的文本扫描器,他有多个构造函数,可以从文件,输入流和字符串中解析出基本类型值和字符串值. public class Main { public static v ...

  2. vmware workstation pro 安装ubantu虚拟机

    参考:https://ywnz.com/linuxaz/3904.html https://www.zhihu.com/search?type=content&q=vmware%20works ...

  3. Centos安装SZRZ方便文件传输

    方便Centos服务器传输文件, 要用到szrz命令, 具体安装如下: # wget  http://freeware.sgi.com/source/rzsz/rzsz-3.48.tar.gz# ta ...

  4. Java钉钉开发_02_免登授权(身份验证)

    源码已上传GitHub: https://github.com/shirayner/DingTalk_Demo 一.本节要点 1.免登授权的流程 (1)签名校验 (2)获取code,并传到后台 (3) ...

  5. hdu-5653 Bomber Man wants to bomb an Array.(区间dp)

    题目链接: Bomber Man wants to bomb an Array. Time Limit: 4000/2000 MS (Java/Others)     Memory Limit: 65 ...

  6. SQL语言方方面面

    1 数据库和SQL 1.1 数据库 DB, DBMS DBMS的种类: 层次性数据库, 关系型数据库, 非关系型数据库 RDBMS, 关系数据库管理系统 1.2 数据库的结构 RDBMS常见的系统结构 ...

  7. 分享几个高效编写JS 的心得

    原则 不要做任何优化除非的确需要优化   任何的性能优化都必须以测量数据为基础,如果你怀疑代码存在性能问题,首先通过测试来验证你的想法.性能优化三问 我还能做哪些工作从而让代码变得更有效率? 流行的J ...

  8. FFMPEG(一) 从V4L2捕获摄像头数据

    系列相关博文: FFMPEG(一) 从V4L2捕获摄像头数据 FFMPEG(二) v4l2 数据格式装换 FFMPEG(三) v4l2 数据编码H264 最近在学习FFMPEG,发现网上的很多例子都是 ...

  9. IDEA发布运行web项目(曾经遇到的项目启动报404)

    问题: 配置: 配置 facets ,此步很重要,配置 web resource directories ,路径配错,就会报 404 ,一定要定位到项目根目录,也就是下面有整个项目源码的地方 下面是配 ...

  10. css 更换浏览器 默认图标

    cursor:url("./images/test.cur"),auto; 只在chrome测试过...