如何将两个数据表列附加到一个 [英] How to append two datatable columns into one

查看:83
本文介绍了如何将两个数据表列附加到一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数据表



Dt1和Dt2



Dt1



Id |姓名

1 | a

2 | b



Dt2



age |城市

11 | kochi

21 |班加罗尔



我想要外出像



Id |姓名|年龄|城市

1 | a | 11 | kochi

2 | b | 21 |班加罗尔



我尝试了什么:



我试过这个< br $>


I have a two dattables

Dt1 and Dt2

Dt1

Id | Name
1 | a
2 | b

Dt2

age | city
11 | kochi
21 | Bangalore

I want the out put like

Id | Name |age |city
1 | a | 11 | kochi
2 | b | 21 | Bangalore

What I have tried:

I have tried this

Dt1= Dt1.Merge(Dt2);

推荐答案

问题是Merge没有做你想做的事:它将一个表附加到另一个表中架构。

所以Merge会给你这个:

The problem is that Merge doesn't do what you want: it appends one table to another with a similar schema.
So Merge will give you this:
1   a
2   b
11  Kochi
21  Bangalore

它不能组合列来形成一个新表,因为它不知道行是如何相关的 - 实际上你的表中没有任何东西以任何方式关联两个行。



要做你想做的事,你将不得不创建一个包含四列的新第三个表,并手动将数据添加到每一行。 (并且希望他们拥有相同数量的行)。



据猜测,您的数据组织存在缺陷,需要认真考虑而不是结合你正在尝试的操作。

It can't "combine columns" to form a new table, because it has no idea how the rows are related - and in fact there is nothing in your tables which in any way relates two rows together.

To do what you want, you will have to create a new third table with four columns, and manually add the data to each row. (And hope like heck they have the same number of rows).

At a guess, your data organisation is flawed, and needs some serious looking at instead of the "combine" operation you are trying for.


另一种选择是将一个列添加到DT2作为ID,并使该ID等于DT1中的相应行。这将定义表之间的关系,这些可以使用LINQ加入。



Stack Overflow =>在C#中内部加入DataTable
[ ^ ]
Another option would be to add a column to DT2 as ID, and have that ID equal to the corresponding line in DT1. This will define the relationship between the tables, and these can be joined utilizing LINQ.

Stack Overflow => Inner Join of DataTables in C#
[^]


这篇关于如何将两个数据表列附加到一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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