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 ...
随机推荐
- PE.微PE
1.老毛桃,大白菜(20180227) ZC:我记得以前 "老毛桃"."大白菜" 之前的版本,在安装好系统之后,是没有捆绑软件的,.现在,老毛桃 安装完系统之后 ...
- linux 压缩和归档
在linux下有几种压缩方式:gzip.bzip2.xz.zip gzip 压缩文件以.gz结尾, 只能压缩文件,不能压缩目录 用法: gzip:/path/to/somefile 用来压缩,完成 ...
- Codeforces 463D Gargari and Permutations:隐式图dp【多串LCS】
题目链接:http://codeforces.com/problemset/problem/463/D 题意: 给你k个1到n的排列,问你它们的LCS(最长公共子序列)是多长. 题解: 因为都是1到n ...
- Delpih - Format
Format是一个很常用,却又似乎很烦的方法,本人试图对这个方法的帮助进行一些翻译,让它有一个完整的概貌,以供大家查询之用: 首先看它的声明:function Format(const Format: ...
- freeMarker(六)——程序开发指南入门
学习笔记,选自freeMarker中文文档,译自 Email: ddekany at users.sourceforge.net 1.创建Configuration实例 首先,你应该创建一个 free ...
- 【Caffe】Ubuntu 安装 Caffe gpu版
安装环境:Ubuntu 16.04lts 64位, gcc5.4 gpu1050ti,cuda8.0,cudnn5.1.10 1. 安装依赖库 sudo apt-get install libprot ...
- [转]PNG8和PNG24的区别
首先我们要知道: 1.png8和png24的根本区别,不是颜色位的区别,而是存储方式不同. 2.png8有1位的布尔透明通道(要么完全透明,要么完全不透明),png24则有8位(256阶)的布尔透明通 ...
- BZOJ4003:[JLOI2015]城池攻占
浅谈左偏树:https://www.cnblogs.com/AKMer/p/10246635.html 题目传送门:https://lydsy.com/JudgeOnline/problem.php? ...
- 不同类型input尺寸设置区别
最近发现为不用类型的input设置相同的尺寸,却得到了不一样的尺寸结果.发现不同类型的input的height和width竟然含义不同.在此小整理一下. (1)button类型 规律 button类型 ...
- Wireshark抓取本地Tcp包(任何数据包)
没有任何一个程序员在做项目的时候不会遇到网络编程的问题,要解决这些问题除了对各种网络协议深入了解之外,还需要掌握各种网络分析工具的用法,不用多说wireshark绝对是这方面的翘楚,可惜的是,wire ...