I am loading three dataset from the database which has
Dataset 1 > Table 1 > 10 rows
Dataset 2 > Table 1 > 3 rows
Dataset 3 > Table 1 > 5 rows
(All dataset has same column name, same datatype) actually they contains the data based on different select condition)
now I want to merge all three dataset into one like this
Dataset 4 > Table 1 > 18 rows
Please help how can I do it
Try the DataSet.Merge Method (DataSet) method. Something like this (not tested):
C#:
dataset1.Merge(dataset2);
DataSet dataset4 = dataset1.Merge(dataset3);
VB.NET:
dataset1.Merge(dataset2)
Dim dataset4 As New DataSet = dataset1.Merge(dataset3)
You can use Tim's suggestion if you're looking to merge the two datasets and all of the containing tables, or you can do something like this for specific tables.
DataTable table1 = new DataTable();
DataTable table2 = new DataTable();
DataTable table3 = new DataTable();
table1.Merge(table2);
table1.Merge(table3);
One (flexible) way is using Enumerable.Union.
In VB.NET:
Dim tblMerged = tbl1.AsEnumerable _
.Union(tbl2.AsEnumerable) _
.Union(tbl3.AsEnumerable) _
.CopyToDataTable()
Tested with:
Dim tbl1 As New DataTable
Dim tbl2 As New DataTable
Dim tbl3 As New DataTable
Dim col1_1 As New DataColumn("Col1", GetType(String))
Dim col2_1 As New DataColumn("Col1", GetType(String))
Dim col3_1 As New DataColumn("Col1", GetType(String))
tbl1.Columns.Add(col1_1)
tbl2.Columns.Add(col2_1)
tbl3.Columns.Add(col3_1)
For i As Int32 = 1 To 10
Dim row = tbl1.NewRow
row(0) = "Tbl1_Value" & i
tbl1.Rows.Add(row)
Next
For i As Int32 = 1 To 20
Dim row = tbl2.NewRow
row(0) = "Tbl2_Value" & i
tbl2.Rows.Add(row)
Next
For i As Int32 = 1 To 50
Dim row = tbl3.NewRow
row(0) = "Tbl3_Value" & i
tbl3.Rows.Add(row)
Next
Dim tblMerged = tbl1.AsEnumerable.Union(tbl2.AsEnumerable).Union(tbl3.AsEnumerable).CopyToDataTable