Solved

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

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

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!
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled

831 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