Solved

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

Posted on 2013-06-11
5
412 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
[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
  • 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

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Suggested Courses

738 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