Solved

C#  Merge 2 dataset based on primary key or 1 column

Posted on 2013-06-11
5
407 Views
Last Modified: 2013-06-12
Hi..
I have 2 datasets I need to merge them for only the rows that match on a single column between them.

Table 1                                  Table 2

ID         Name                         ID     Name
1            John                           1       Bob
2            Jane                           3        Fred


Want to merge table 1 with table 2   - only  Bob should be added to table 1

What's the best way to do this a loop or can I do via LINQ

thx
0
Comment
Question by:JElster
  • 3
  • 2
5 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39239821
Where does this data live? Is it literally in a DataSet, or is it in the database and you are retrieving it via LINQ (or another method)?
0
 
LVL 1

Author Comment

by:JElster
ID: 39239825
It's now in a DataSet...  complex queries add to dataset to be export to CSV
Can't mod queries
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39239831
So what would Bob's ID be once he was merged into Table 1?
0
 
LVL 1

Author Comment

by:JElster
ID: 39239836
Doesn't matter ID isn't exported

Maybe something like this...
 DataSet dsTEMP = dsUsers.Clone();

                foreach (DataRow dr in dsUsers.Tables[0].Rows)
                {
                    DataRow[] results = dsOther.Tables[0].Select("ID = '" +dr["ID"].ToString()+"'");

                    if (results.Count() > 0)
                    {
               
                        foreach (DataRow _d in results)
                        {

                            dsTEMP.Tables[0].ImportRow(_d);
                        }
                    }

                }
               
                dsUsers.Merge(dsTEMP);
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39239915
See if this approach works for you. I'm including how I populated the tables.

static void Main(string[] args)
{
    DataSet table1 = new DataSet();
    DataSet table2 = new DataSet();
    DataSet dsTEMP = new DataSet();

    table1.Tables.Add(new DataTable());
    table1.Tables[0].Columns.Add("ID", typeof(int));
    table1.Tables[0].Columns.Add("Name", typeof(string));
    table1.Tables[0].Rows.Add(1, "John");
    table1.Tables[0].Rows.Add(2, "Jane");

    table2.Tables.Add(new DataTable());
    table2.Tables[0].Columns.Add("ID", typeof(int));
    table2.Tables[0].Columns.Add("Name", typeof(string));
    table1.Tables[0].Rows.Add(1, "Bob");
    table1.Tables[0].Rows.Add(3, "Fred");

    var query = from row1 in table1.Tables[0].AsEnumerable()
                join row2 in table2.Tables[0].AsEnumerable()
                on row1.Field<int>("ID") equals row2.Field<int>("ID")
                select row2;

    dsTEMP.Tables.Add(table1.Tables[0].AsEnumerable()
                                      .Concat(query)
                                      .CopyToDataTable());
}

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…

679 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