Click here to Skip to main content
15,894,328 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can I combine two different datasets into a single one?

Which do not have any common rows
Posted
Updated 23-May-13 22:33pm
v2
Comments
Madhav Hatwalne 24-May-13 4:21am    
why you want such this?
lukeer 24-May-13 4:22am    
Maybe they have common columns?
lukeer 24-May-13 4:23am    
Do they have common columns?
Arun Vasu 24-May-13 4:45am    
give some more idea. why you need to this case. if so i can help you.

You can most certainly combine two datasets into one dataset regardless if they have common rows and/or disparate/matching columns. It depends on how the schema definition is set up (primary key?)and what the MissingSchemaAction[^] value is when one calls the DataSet.Merge(DataSet, Boolean, MissingSchemaAction)[^] method.

Regards,
— Manfred
 
Share this answer
 
v3
From your question is not clear if you really need to combine two datasets (that is set of tables) or simply two datatables (set of rows).
In both cases the answer is: "yes, of course", because you can always build a resulting set, namely the union of the input ones (e.g. in datatables case, you build a resulting datatable having all the columns and rows of both the input datatables).
The problem is: should such 'combine' operation produce a meaningful result?
 
Share this answer
 
There is no "simple solution" - what you are trying to do is a complex process, even with pretty small data samples.
For example:
C#
DataTable dt1 = new DataTable();
dt1.Columns.Add("ID");
dt1.Columns.Add("Name");
dt1.Rows.Add(1, "Fred");
dt1.Rows.Add(2, "Mark");
dt1.Rows.Add(3, "Michelle");
DataTable dt2 = new DataTable();
dt2.Columns.Add("ID");
dt2.Columns.Add("Age");
dt2.Rows.Add(1, "23");
dt2.Rows.Add(2, "23");
dt2.Rows.Add(3, "25");

var result = from d1 in dt1.AsEnumerable()
             join d2 in dt2.AsEnumerable() on d1["ID"] equals d2["ID"]
             select new
                {
                    Id = d1["ID"],
                    Name = d1["Name"],
                    Age = d2["Age"]
                };
DataTable dt3 = result.ToList().ToDataTable();
Creates two (related) data tables, and uses Linq and an extension method to generate a combined table.
(The extension method is available here: Converting a List to a DataTable[^])

But this is dependant on the two tables having related information. When you are dealing with separate DataSets which will likely have multiple tables in each, it becomes even more complicated.

If possible it would be better to do this at the data source - your database for example.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900