本文介绍了从数据表中删除行的例外情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从数据表中删除记录时我收到异常

集合被修改枚举操作可能无法执行。

我的代码低于



public DataTable RemoveChargeWithZeroAmount(String pDiscomCode,RequestParameters pRequestParameters,DataTable pDataTable)

{

foreach(pDataTable.Rows中的DataRow费用)

{

if(Convert.ToDouble(Charge [AMOUNT])== 0.00 || Convert.ToDouble(Charge [AMOUNT])== 0 || Convert.ToDouble(Charge [AMOUNT])== 0.0)

{

pDataTable.Rows.Remove(Charge);

}

}

pDataTable.AcceptChanges();

返回pDataTable;

}

when deleteing record from data table i am getting Exception as
collection was modified enumeration operation might not execute.
My Code is below

public DataTable RemoveChargeWithZeroAmount(String pDiscomCode, RequestParameters pRequestParameters, DataTable pDataTable)
{
foreach (DataRow Charge in pDataTable.Rows)
{
if (Convert.ToDouble(Charge[AMOUNT]) == 0.00 || Convert.ToDouble(Charge[AMOUNT]) == 0 || Convert.ToDouble(Charge[AMOUNT]) == 0.0)
{
pDataTable.Rows.Remove(Charge);
}
}
pDataTable.AcceptChanges();
return pDataTable;
}

推荐答案

public DataTable RemoveChargeWithZeroAmount(String pDiscomCode, RequestParameters pRequestParameters, DataTable pDataTable)
 {
   foreach (DataRow Charge in pDataTable.Rows)
    {
      if (Convert.ToDouble(Charge[AMOUNT]) == 0.00 || Convert.ToDouble(Charge[AMOUNT]) == 0 || Convert.ToDouble(Charge[AMOUNT]) == 0.0)
       {
          pDataTable.Rows.Remove(Charge);

       }
        pDataTable.AcceptChanges();
    }
   return pDataTable;
  }





或者您可以通过数据表反向迭代: -





or you can reverse iterate through datatable as:-

foreach (DataRow Charge in pDataTable.Rows.count - 1)
{
    if (Convert.ToDouble(Charge[AMOUNT]) == 0.00 || Convert.ToDouble(Charge[AMOUNT]) == 0 || Convert.ToDouble(Charge[AMOUNT]) == 0.0)
       {
          pDataTable.Rows.Remove(Charge);

       }
     pDataTable.AcceptChanges();
}


这篇关于从数据表中删除行的例外情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 18:21