• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C#Collectionwasmodified;enumerationoperationmaynotexecute

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

一、问题描述

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

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap