2数据表之间的差异数据表、差异

2023-09-12 21:22:32 作者:一滴小雨珠

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

I have two datatables. DataTable dtRequired and DataTable dtResult.

我要输出包含没有present在dtResponse,但被发现在dtRequired行的数据表。

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

办法1 我们一直在使用在以下网址的http:// weblogs.sqlteam.com/davidm/archive/2004/01/19/739.aspx 。 该算法想通是较慢的人在我们的评测之一。

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.

办法2 所以,我想,以取代上述算法中的东西,多数民众赞成如下所述。 dtRequired进行索引我米使用下面的列的查找行。

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);
                }
            }                        
        }
    }

上面一块但是花费的时间比采取办法1 的时间更长。 办法2 大约需要3秒的dtResult与1250行和dtRequired有4500行。

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?

推荐答案

这是一个的链接以使用LINQ到数据集以获得出现在两个数据表中的行一个MSDN页面。这个例子使用的相交的。我想你可以使用的除了的修改来代替。我不知道如果表现会更好与否。

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.