一、问题描述

  在做 数组、列表、集合遍历时,可能我们会遇见这个问题。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.io.InvalidClassException 异常解决, 实现Serializable接口的注意事项

    解决方案: 在类中显式指定 private static final long serialVersionUID = 42L; 类实现序列化接口, 进行序列化反序列化的时候, 抛出 java.io.I ...

  2. SQLServer 一些有用的语句

    SET STATISTICS TIME ON 记录查询的相关数据 生成随机Guid SELECT NewID() 按照某一列排序并生成序号 select Row_Number() OVER (ORDE ...

  3. Android之ExpandableList扩展用法(基于BaseExpandableListAdapter)

    1.简介 基于基于BaseExpandableListAdapter扩展的ExpandableList用法,现在网上流行的主要有两种:第一种是向BaseExpandableListAdapter传入两 ...

  4. 【遍历二叉树】01二叉树的前序遍历【Binary Tree Preorder Traversal】

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,返回他的前序遍历的 ...

  5. H264视频编码成MP4文件

    firehood的专栏 Wince嵌入式开发       目录视图 摘要视图 订阅 赠书 | AI专栏(AI圣经!<深度学习>中文版)      每周荐书:Kotlin.分布式.Keras ...

  6. 【VS】VS开发中遇到的问题的总结

    1.  VS中经常会出现无法解析的外部符号,还有LINK ERROR 2019等 这类问题如果检查代码没有错误,很大概率就是lib文件错误.调试程序找出问题函数,再找出问题函数使用到的lib文件,在项 ...

  7. 【Caffe】Ubuntu 安装 Caffe gpu版

    安装环境:Ubuntu 16.04lts 64位, gcc5.4 gpu1050ti,cuda8.0,cudnn5.1.10 1. 安装依赖库 sudo apt-get install libprot ...

  8. rman命令详解(三)

    1. Report 命令用户判断数据库的当前可恢复状态和提供数据库备份的特定信息1.1 指定最近没有备份的数据文件查询3天内没有备份过的表空间,可以用如下命令:RMAN> report need ...

  9. node-webkit开发基本步骤

    详情请查看:http://www.heiboard.com/?p=2091

  10. 【转】 Pro Android学习笔记(四十):Fragment(5):适应不同屏幕或排版

    目录(?)[-] 设置横排和竖排的不同排版风格 改写代码 对于fragment,经常涉及不同屏幕尺寸和不同的排版风格.我们在基础小例子上做一下改动,在横排的时候,仍是现实左右两个fragment,在竖 ...