2个数据表之间的差异 [英] difference between 2 datatables

查看:100
本文介绍了2个数据表之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数据表。 DataTable dtRequired和DataTable dtResult。



我想输出一个datatable,它包含dtResponse中不存在但在dtRequired中找到的行。



方法1
我们一直在使用以下url指定的算法 http://weblogs.sqlteam.com/davidm/archive/2004/01/19/739.aspx
这个算法被认为是我们剖析中较慢的算法之一。



方法2
所以,我试图用下面描述的东西替换上述算法。
dtRequired在我使用的列上索引到查找行。

  if(dtResult.Rows.Count> 0)
{
lock(dtResult)
{
DataRow rowfound = null; (int i = 0; i< dtResult.Rows.Count; i ++)
{
DataRow row = dtResult.Rows [i];

rowfound = dtRequired.Rows.Find(new object [] {row [0],row [1],row [2],row [3],row [4],row [5],row [6 ],行[7],行[8]});
if(rowfound!= null)
{
dtRequired.Rows.Remove(rowfound);
}
}
}
}

然而,上面的部分花费比方法1 花费的时间更长。
方法2 需要〜3秒的dtResult,1250行,dtRequired需要4500行。



方法有问题吗上文提到的?有没有更好的方法实现这一点?

解决方案

这是一个链接到使用LINQ to DataSet的MSDN页面,以获取两个数据表中显示的行。此示例使用相交。我想你可以使用除 之外的修改它。我不知道演出是否会更好。


I have two datatables. DataTable dtRequired and DataTable dtResult.

I want to output a datatable that contains rows that were not present in dtResponse but were found in dtRequired.

Approach 1 We have been using the algorithm specified at the following url http://weblogs.sqlteam.com/davidm/archive/2004/01/19/739.aspx. And this algorithm figured to be one of the slower ones in our profiling.

Approach 2 So, I tried to replace the above algo with something thats described below. dtRequired is indexed on the columns I m using below to Find the row.

    if (dtResult.Rows.Count > 0)
    {
        lock (dtResult)
        {
            DataRow rowfound = null;
            for (int i = 0; i < dtResult.Rows.Count; i++)
            {
                DataRow row = dtResult.Rows[i];
                rowfound = dtRequired.Rows.Find(new object[] { row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8] });
                if (rowfound != null)
                {
                    dtRequired.Rows.Remove(rowfound);
                }
            }                        
        }
    }

The above piece however is taking longer than the time taken by Approach 1. Approach 2 takes ~3 secs for dtResult with 1250 rows and dtRequired with 4500 rows.

Is something wrong with the approach I mentioned above? Is there any better approach of achieving this?

解决方案

This is a link to a MSDN page that use LINQ to DataSet to obtain the rows that appears in both datatables. This example use Intersect. I think you could modify it using except instead. I don't know if the performance will be better or not.

这篇关于2个数据表之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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