比较两个DataTable [英] Comparing Two DataTables

查看:97
本文介绍了比较两个DataTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个DataTable,我需要比较两者。我需要的是Table1中的一行与Table2中的一行匹配(意味着它们包含相同的数据)然后删除表1中的行



这就是我现在所拥有的,但 运行时间太长。我需要知道这是否正确,或者是否有更快的方法来做到这一点。谢谢

列表< DataRow> rowsToRemove = new List< DataRow>(); 

foreach(newTable.Rows中的DataRow r)
{
foreach(table.Rows中的DataRow r2)
{
if(r == r2) )
{
rowsToRemove.Add(r);
}
}
}

foreach(dataRow r in rowsToRemove)
{
newTable.Rows.Remove(r);
}
newTable.AcceptChanges();

解决方案

这就是我现在所拥有的,但需要
<强>跑得太久了。我需要知道这是否正确,或者是否有更快的方法来做到这一点。谢谢


https://social.msdn.microsoft.com/Forums/vstudio/en-US/3aa9d793-4cd8-492f-b860-36fc6faa36be/你能加快速度吗?论坛= vbgeneral


I have two DataTables, and I need to compare the two. What I need is when a row in Table1 matches a row in Table2 (meaning they contain the same data) then delete the row in Table 1

This is what I have right now, but it takes far too long to run. I need to know if this is even correct, or if there is a faster way to do this. Thanks

List<DataRow> rowsToRemove = new List<DataRow>();

foreach (DataRow r in newTable.Rows)
{
    foreach (DataRow r2 in table.Rows)
    {
        if(r == r2)
        {
            rowsToRemove.Add(r);
        }
    }
}

foreach (DataRow r in rowsToRemove)
{
    newTable.Rows.Remove(r);
}
newTable.AcceptChanges();

解决方案

This is what I have right now, but it takes far too long to run. I need to know if this is even correct, or if there is a faster way to do this. Thanks

https://social.msdn.microsoft.com/Forums/vstudio/en-US/3aa9d793-4cd8-492f-b860-36fc6faa36be/can-you-speed-up-the-below-function?forum=vbgeneral


这篇关于比较两个DataTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆