将动态变量绑定到datatable并递归处理每个变量

时间:2017-01-12 07:25:54

标签: c# recursion

我正在处理来自excel的一些行并查询它们并存储在一些变量(12 var s)中,如下所示。 dtremoverows包含来自excel的数据。

 private System.Data.DataTable RemoveRows(System.Data.DataTable dtremoverows, bool mage)
 {
        System.Data.DataTable dtreturnrows = new System.Data.DataTable();
        int rowcount = dtremoverows.Rows.Count;
        var RemoveRows1 = dtremoverows.AsEnumerable().Where(r => r["CRE_DATE"].ToString() == "" && r["EMP_NO / Emp_Name"].ToString() == "").ToList();
        var RemoveRows2 = dtremoverows.AsEnumerable().Where(r => r["CRE_DATE"].ToString() == "" && r["QTY"].ToString() == "").ToList();
        var RemoveRows3 = dtremoverows.AsEnumerable().Where(r => r["CRE_DATE"].ToString().Contains("Total in Group:") || r["QTY"].ToString().Contains("Total in Group:") || r["EMP_NO / Emp_Name"].ToString().Contains("Total in Group:") || r["WO_NO"].ToString().Contains("Total in Group:") || r["ROLE_CODE"].ToString().Contains("Total in Group:") || r["CATALOG_NO"].ToString().Contains("Total in Group:")).ToList();
        var RemoveRows4 = dtremoverows.AsEnumerable().Where(r => r["CRE_DATE"].ToString().Contains("Print Date/Time") || r["QTY"].ToString().Contains("Print Date/Time") || r["EMP_NO / Emp_Name"].ToString().Contains("Print Date/Time") || r["WO_NO"].ToString().Contains("Print Date/Time") || r["ROLE_CODE"].ToString().Contains("Print Date/Time") || r["CATALOG_NO"].ToString().Contains("Print Date/Time")).ToList();
        var RemoveRows5 = dtremoverows.AsEnumerable().Where(r => r["EMP_NO / Emp_Name"].ToString() == "" && r["QTY"].ToString() == "").ToList();
        var RemoveRows6 = dtremoverows.AsEnumerable().Where(r => r["CRE_DATE"].ToString() == "0" || r["EMP_NO / Emp_Name"].ToString() == "0" || r["CATALOG_NO"].ToString() == "0").ToList();
        var RemoveRows7 = dtremoverows.AsEnumerable().Where(r => r["CATALOG_NO"].ToString().Contains("Total Amount") || r["QTY"].ToString().Contains("Total Resource Count")).ToList();
        var RemoveRows8 = dtremoverows.AsEnumerable().Where(r => r["WO_NO"].ToString() == "" || r["QTY"].ToString() == "" || r["QTY"].ToString() == "0").ToList();
        var RemoveRows9 = dtremoverows.AsEnumerable().Where(r => r["EMP_NO / Emp_Name"].ToString() == "<None>" && (r["QTY"].ToString() == "" || r["CATALOG_NO"].ToString() == "")).ToList();
        var RemoveRows10 = dtremoverows.AsEnumerable().Where(r => r["WO_NO"].ToString() == "Work Ord" && r["EMP_NO / Emp_Name"].ToString() == "Employee #/Sales Part #").ToList();
        var RemoveRows11 = dtremoverows.AsEnumerable().Where(r => r["QTY"].ToString() == "0" || r["QTY"].ToString() == "").ToList();
        var RemoveRows12 = dtremoverows.AsEnumerable().Where(r => r["CRE_DATE"].ToString() == "" && r["WO_NO"].ToString() == "" && r["EMP_NO / Emp_Name"].ToString() == "" && r["ROLE_CODE"].ToString() == "" && r["CATALOG_NO"].ToString() == "" && r["QTY"].ToString() == "" && r["PLAN_LINE_NO"].ToString() == "" && r["WORK_ORDER_COST_TYPE"].ToString() == "" && r["WORK_ORDER_ACCOUNT_TYPE"].ToString() == "" && r["ORG_CODE"].ToString() == "" && r["CMNT"].ToString() == "").ToList();
 }

然后我将每个变量添加为循环。我可以将它用作单个递归循环,因为我猜我的编码(下面)已被弃用。

 try
        {

            if (RemoveRows1.Count() != 0)
            {
                foreach (var row in RemoveRows1)
                {
                    dtremoverows.Rows.Remove(row);

                }
            }
        }
        catch { }

        try
        {
            if (RemoveRows2.Count() != 0)
            {
                foreach (var row in RemoveRows2)
                {
                    dtremoverows.Rows.Remove(row);
                }
            }
        }
        catch { }

......... 依此类推..直至RemoveRows12然后合并为:

dtreturnrows.Merge(dtremoverows);

这有效,但我真的需要更好的编码方法。如果您有任何建议,请回复。

2 个答案:

答案 0 :(得分:1)

仔细查看条件RemoveRows1RemoveRows2等的代码段,以便使用大量变量。为什么不合并条件?我想你必须尝试这样的事情:

 var rowsToRemove = dtremoverows.AsEnumerable()
                                .Where(r => 
                                (/* Condition for RemoveRows1 */) ||
                                (/* Condition for RemoveRows2 */) ||
                                (/* Condition for RemoveRows3 */) ||
                                (/* Condition for RemoveRows4 */) );
// And so on you can add conditions so that you will get a collection 
// of all rows that should be deleted from the table
// then iterate through those rows and perform delete like this:

foreach (var row in rowsToRemove)
{
     dtremoverows.Rows.Remove(row);
}

请注意:这只是一个建议,不是问题的确切答案,但它不仅仅是一个评论,这就是为什么发布作为答案。如果违反规则,请原谅我。

很高兴为您提供帮助。

答案 1 :(得分:0)

拇指角色是你不应该使用递归,因为它不可读且不易理解,如果它深入到递归循环中,可能会导致堆栈溢出异常。

如果可以的话,你应该使用简单的循环来完成你的迭代。