• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 234
  • Last Modified:

Comparing 2 datatables

I have created a dataset, added one table(Table1) to it, which is populated with the details of the folders in a particular directory. I want to add another table(Table2) which will get folder details from a table in a sql database. I would then like to compare the 2 tables and if there are rows(based on column -foldername), that are present in Table2 but not in Table1 then append only those(that are present in Table2 but not in Table1) to Table1. I would then bind Table 1 to a grid. What would be a good way to achieve the above. Code samples will be really helpful.
Thanks in advance.
0
Rintu
Asked:
Rintu
  • 5
  • 2
1 Solution
 
_TAD_Commented:


                  DataTable dt1 = new DataTable();
                  DataTable dt2 = new DataTable();



                  //Load Data into DataTables


                  DataRow resultant;
                  
                  foreach(DataRow dr in dt2.Rows)
                  {
                        resultant = dt1.Rows.Find(dr["foldername"].ToString());
                        if(resultant == null)
                              dt1.Rows.Add(dr);
                  }

                  dataGrid1.DataSource = dt1;
0
 
_TAD_Commented:


In a nutshell, the above code has two DataTables

dt1 and dt2


the code also creates a new dataRow for temperary storage.


it then loops through each data row in dt2 and grabs the data from column "foldername" and tries to find that value in data table 1 (dt1).

if it finds the value then it sets the resultant (a datarow) to the value of the current datarow.  if it does not find a value, it sets the resultant to null.

If the resultant is null (i.e. the row was not found in dt1) then the current datarow in dt2 is added to dt1.

0
 
RintuAuthor Commented:
The above solution does not seem to work in my case as the col. FolderName is not the primarykey. Can you please suggest an alternative  ?
Thanks in advance.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
_TAD_Commented:


Do you have a primary key specified?

If not, the only alternative I see is to go through each and every directory in both tables and see if that value exists in the other table.

If your data tables have less than 100 rows each then you should not notice a performance hit, but many more than that and you may notice some delay.



I suppose you could create a query string and then execute a filter and see how many rows are available.

I haven't played with the filter command much, but I think it might work.

I'll play around with some code this afternoon and see what I can come up with that doesn't use the "find' function
0
 
_TAD_Commented:


Are each of the rows in your data table unique?

If the rows are unique, then you can use the row as your key since the Find() function uses an object as a key.


               DataTable dt1 = new DataTable();
               DataTable dt2 = new DataTable();



               //Load Data into DataTables


               DataRow resultant;
               
               foreach(DataRow dr in dt2.Rows)
               {
                    resultant = dt1.Rows.Find(dr);
                    if(resultant == null)
                         dt1.Rows.Add(dr);
               }

               dataGrid1.DataSource = dt1;
0
 
_TAD_Commented:


The only problem with the code above is that I don't know if you will ever find a match.  I don't know if the find function is actually using the values of the data row or if it is using the datarow object identifier or possibly its hash value.
0
 
RintuAuthor Commented:
No, the rows are not unique. However I do want to ensure that only rows which have a unique value for the the col.'foldername' are added to the table.
Thanks
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now