[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Comparing 2 datatables

Posted on 2003-10-27
7
Medium Priority
?
233 Views
Last Modified: 2010-04-16
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
Comment
Question by:Rintu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
7 Comments
 
LVL 22

Expert Comment

by:_TAD_
ID: 9630375


                  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
 
LVL 22

Expert Comment

by:_TAD_
ID: 9630396


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
 

Author Comment

by:Rintu
ID: 9635618
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 22

Expert Comment

by:_TAD_
ID: 9635736


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
 
LVL 22

Expert Comment

by:_TAD_
ID: 9644377


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
 
LVL 22

Accepted Solution

by:
_TAD_ earned 750 total points
ID: 9644415


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
 

Author Comment

by:Rintu
ID: 9644416
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question