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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
VulpesRank #55 · 29.2K reputationPosted Sep 1, 2011, 1:23 AM EDT
// 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
Sam HobbsRank #107 · 20.5K reputationPosted Aug 31, 2011, 5:44 PM EDT
Black Diamond191 reputationPosted Aug 31, 2011, 5:57 PM EDT
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?
Black Diamond191 reputationPosted Sep 1, 2011, 2:17 AM EDT
Thank a lot for your code it work's perfectly ....