帮助“”集合已修改;枚举操作可能不执行“。 [英] Help with "Collection was modified; enumeration operation might not execute."
本文介绍了帮助“”集合已修改;枚举操作可能不执行“。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我使用下面的代码,我得到一个集合被修改;枚举操作可能不执行错误。如果我理解这正确,我认为这意味着我不能枚举这个数据表,并从中删除行同时。我的问题是,我还能做些什么来实现从我的数据表删除某些行的结果?
这是我的代码:
When I use the following code, I'm getting a "Collection was modified; enumeration operation might not execute" error. If I understand this correctly, I think it means that I can't enumerate over this datatable and delete rows from it at the same time. My question is, what else can I do to achieve the result of deleting certain rows from my datatable? Here's my code:
// Now let's loop through the datatable and find any account with no
// LYSMKWh value and less than 4000 KWh current usage. We'll remove those
// from the datatable.
currentRow = 0;
foreach (DataRow row in resultsDT.Rows)
{
if ((string)resultsDT.Rows[currentRow]["LYSMKWh"] == "0")
{
int intKWh = Convert.ToInt32(resultsDT.Rows[currentRow]["KWH"]);
if (intKWh < 5000)
{
resultsDT.Rows[currentRow].Delete();
}
}
resultsDT.AcceptChanges();
currentRow++;
}
推荐答案
SO从将此转换为评论...
您只需在集合上向后循环...
这篇关于帮助“”集合已修改;枚举操作可能不执行“。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文