Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
two data table have same struct and both primary key have been set.
both table have a flag column , initially the value is blank.
compare each row in two datatable and set this flag, 1 mean has the same row, 0 means not.
table is huge, is there in efficient way to do this?

What I have tried:

What have you tried

What have you tried

What have you tried
Posted
Updated 24-Sep-17 22:44pm
Comments
Richard MacCutchan 25-Sep-17 3:56am    
Yes, what have you tried?
855 25-Sep-17 4:37am    
i tried the transitional way, use two loops, it's very slow.
855 25-Sep-17 4:37am    
i tried the transitional way, use two loops, it's very slow.
855 25-Sep-17 4:35am    
additional, need to compare each column in the data table.
Karthik_Mahalingam 25-Sep-17 4:38am    
Use Improve question to add more info to the question.

1 solution

use DataTable.Select Method (String, String) (System.Data)[^] to find the rows in the target table so that you shall eliminate the looping.

try this, take care of validation and casting etc.

foreach (DataRow row in dt1.Rows)
           {
               var id = row["YourIDColumn"];
               DataRow[] rows = dt2.Select("YourIDColumn ='" + id + "'");
               if (rows.Length == 1)
               {
                   if (rows[0]["YourIDColumn"] == id)
                   {
                       // your code
                   }
                   else
                   {
                       // your code
                   }
               }
               else
               {
                   // item not found code..
               }
           }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900