快速从数据表中删除在另一个数据表中找不到的行

时间:2013-07-02 06:33:14

标签: c#

这里我有两个数据表dtPartMaster([Part Number Exported])和dtPartBackUp([Part Number Backup],[weight]) 现在我想删除dtPartBackUp中的行,其中[part Number Backup]在dtPartMaster中不存在[[Part Number Exported]) 我的逻辑是这样,但处理billon记录需要很长时间

 button1_click()
    {  int count = 0;
//line remove for clearity

    foreach (DataRow dr in dtPartBackUp.Rows)
    {
      if (!CheckPartNumber(dr["Part Number Backup"].ToString(), dtPartMaster))
       {
           dtPartBackUp.Rows[count].Delete();
       }

    count++;
    }

    dtPartBackUp.AcceptChanges();// to accept changes

    }

 private bool CheckPartNumber(string PartNumber, DataTable dt)
        {
            bool flag = false;
            DataRow dr = dt.Select("[Part Number Exported] Like '" + PartNumber + "'").FirstOrDefault();
            if (dr != null)
                flag = true;

            dr = null;
            return flag;

        }
提前谢谢 阿姆里特

2 个答案:

答案 0 :(得分:0)

试试这个(未经测试),

这应该返回基于id列的dt1和dt2中的行列表(我正在考虑你的id和id2列是字符串类型)

var rows = (from r1 in dt1.AsEnumerable()
            join  r2 in dt2.AsEnumerable() on r1.Field<string>("id") equals r2.Field<string>("id2")
            select r1).ToList();

答案 1 :(得分:0)

for (i = 0;i< dtPartBackUp.Rows.count;i++)
    {
      if (!CheckPartNumber(dr["Part Number Backup"].ToString(), dtPartMaster))
       {
           dtPartBackUp.Rows.removeat(i);
i--;
       }


    }

    dtPartBackUp.AcceptChanges();// to accept changes

    }

 private bool CheckPartNumber(string PartNumber, DataTable dt)
        {
            bool flag = false;
            DataRow dr = dt.Select("[Part Number Exported] Like '" + PartNumber + "'").FirstOrDefault();
            if (dr != null)
                flag = true;

            dr = null;
            return flag;

        }