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?