H!,
How to comparing two different table rows using nested loop to prevent duplication?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Sep 1, 2011, 5:23 AM
// insert code to create and fill a new DataRow, row2, of dataTable2
// now check whether there's already a similar row in dataTable1
bool hasSameRow = false;
foreach(DataRow row1 in dataTable1.Rows)
{
bool hasSameColumns = true;
for(int i = 0; i < row1.ItemArray.Length; i++)
{
object item1 = row1.ItemArray[i];
object item2 = row2.ItemArray[i];
if(item1 == null && item2 == null)
continue;
else if(Convert.IsDBNull(item1) && Convert.IsDBNull(item2))
continue;
else if(item1.ToString() != item2.ToString())
{
hasSameColumns = false;
break;
}
}
if(hasSameColumns)
{
hasSameRow = true;
break;
}
}
if (!hasSameRow) dataTable2.Rows.Add(row2); // OK to add it to dataTable2
Black DiamondPosted Sep 1, 2011, 6:17 AM
Thank a lot for your code it work's perfectly ....
Black DiamondPosted Aug 31, 2011, 9:57 PM
I just want to compare two different table rows to avoid duplicate in the two tables, i want that if one record is already add in table1, it should not
be added in table2, what should i do?, is that right to use nested loop to read one of each table?
Can anyone help me out this problem?
Sam HobbsPosted Aug 31, 2011, 9:44 PM