帮助“”集合已修改;枚举操作可能不执行“。

问题描述:

当我使用下面的代码,我得到一个集合被修改;枚举操作可能不执行错误。如果我理解这正确,我认为这意味着我不能枚举这个数据表,并从中删除行同时。我的问题是,我还能做些什么来实现从我的数据表删除某些行的结果?
这是我的代码:

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从将此转换为评论...

您只需在集合上向后循环...