Solved

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

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

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 74

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 74

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
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.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

747 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

12 Experts available now in Live!

Get 1:1 Help Now