合并 2 个数据表并存储在一个新的数据表中 [英] Merge 2 DataTables and store in a new one

查看:22
本文介绍了合并 2 个数据表并存储在一个新的数据表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有 2 个数据表(dtOne 和 dtTwo)并且我想合并它们并将它们放在另一个数据表 (dtAll) 中.我怎样才能在 C# 中做到这一点?我在数据表上尝试了 Merge 语句,但这返回无效.合并是否保留数据?例如,如果我这样做:

If I have 2 DataTables (dtOne and dtTwo) and I want to merge them and put them in another DataTable (dtAll). How can I do this in C#? I tried the Merge statement on the datatable, but this returns void. Does Merge preserve the data? For example, if I do:

 dtOne.Merge(dtTwo);

dtOne 会改变还是 dtTwo 会改变,如果其中任何一个改变,这些改变会保留吗?

Does dtOne change or does dtTwo change and if either one changes, do the changes preserve?

我知道我不能这样做,因为 Merge 返回 void,但我希望能够将 dtOne 和 dtTwo 的合并存储在 dtAll 中:

I know I can't do this because Merge returns void, but I want to be able to store the Merger of both dtOne and dtTwo in dtAll:

//Will Not work, How do I do this
dtAll = dtOne.Merge(dtTwo);

推荐答案

Merge 方法从第二个表中获取值并将它们与第一个表合并,因此第一个现在将保存两者的价值.

The Merge method takes the values from the second table and merges them in with the first table, so the first will now hold the values from both.

如果要保留两个原始表,可以先复制原始表,然后合并:

If you want to preserve both of the original tables, you could copy the original first, then merge:

dtAll = dtOne.Copy();
dtAll.Merge(dtTwo);

这篇关于合并 2 个数据表并存储在一个新的数据表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆