将表从 3 个数据集合并到 1 个

发布于 2024-12-11 19:14:09 字数 297 浏览 0 评论 0原文

我正在从数据库加载三个数据集

Dataset 1 > Table 1 > 10 rows
Dataset 2 > Table 1 > 3 rows
Dataset 3 > Table 1 > 5 rows

(所有数据集具有相同的列名,相同的数据类型),实际上它们包含基于不同选择条件的数据) 现在我想将所有三个数据集合并为一个这样的数据集

数据集4>表1 18行

请帮忙我该怎么做

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

何止钟意 2024-12-18 19:14:09

尝试DataSet.Merge 方法 (DataSet) 方法。像这样的东西(未测试):

C#:

dataset1.Merge(dataset2);

DataSet dataset4 = dataset1.Merge(dataset3);

VB.NET:

dataset1.Merge(dataset2)

Dim dataset4 As New DataSet = dataset1.Merge(dataset3)    

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)    
狼亦尘 2024-12-18 19:14:09

如果您希望合并两个数据集和所有包含的表,则可以使用蒂姆的建议,或者您可以对特定表执行类似的操作。

DataTable table1 = new DataTable();
DataTable table2 = new DataTable();
DataTable table3 = new DataTable();

table1.Merge(table2);
table1.Merge(table3);

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);
冷月断魂刀 2024-12-18 19:14:09

一种(灵活)方法是使用 Enumerable.Union

在 VB.NET 中:

Dim tblMerged = tbl1.AsEnumerable _
                    .Union(tbl2.AsEnumerable) _
                    .Union(tbl3.AsEnumerable) _
                    .CopyToDataTable()

测试使用:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文