foreach循环遍历list出错

今天在使用foreach循环遍历list集合时,出现Collection was modified; enumeration operation may not execute.这个错误,查了半天才发现是当想要修改list集合时,不能使用foreach,因为foreach是取只读的,在取的时候数据不能变(包括修改,删除,添加等),所以要写for循环。

 在for循环里面,删除操作结束,一定要 i-- 操作。不然会漏掉部分元素。

 

for

 

 

(int i = 0; i < result.Count;i++ )

                {

                   

 

DateTime tTGL = Convert.ToDateTime(result[i].TTGL, dtFormat);

                   

 

if (DateTime.Compare(date_From, tTGL) > 0 || DateTime.Compare(date_To, tTGL) < 0)

                    {

                        result.Remove(result[i]);

        i--;

                    }

                }

转载于:https://www.cnblogs.com/muzilingling/p/8241775.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值