Solved

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

Posted on 2013-06-11
5
396 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Log4Net custom Appender not being fired. 5 75
Problem with SqlConnection 4 160
New to SOAP and need some direction 10 46
In WPF / C# binding a local database in code behind 1 24
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

895 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now