首页 新闻 会员 周边

datatable合并并处理相同行

0
悬赏园豆:20 [已关闭问题] 关闭于 2011-04-09 18:40

我现在有两个datatable A和B

A

name      count

"张三"         1

“李四”          1  

B

name           count

"张三"               2

“李四”                3

“王五”                  1

我想合并为

name              count

“张三”                     3

”李四“                     4

“王五”                      1

怎么做呢?

马战鹏的主页 马战鹏 | 菜鸟二级 | 园豆:432
提问于:2011-04-02 09:10
< >
分享
所有回答(6)
0

创建一个新的datatable c

用foreaech 分别取出 ab中的每一行 添加到c

死白的man | 园豆:2135 (老鸟四级) | 2011-04-02 09:15
相同行的判断,列书很多的话怎么判断?
支持(0) 反对(0) 马战鹏 | 园豆:432 (菜鸟二级) | 2011-04-02 09:20
创建c 让c=a;
遍历b表 如果b中的name在c中存在 就count相加 如果不存在就插入
支持(0) 反对(0) 死白的man | 园豆:2135 (老鸟四级) | 2011-04-02 10:20
此方法可行
支持(0) 反对(0) soarfe | 园豆:146 (初学一级) | 2011-04-08 13:37
0

能在数据库那边处理么?比如用group by

六芒星 | 园豆:627 (小虾三级) | 2011-04-02 09:58
0

1、在数据库对其 A、B表进行分组统计,再新建C表,把筛选后的数据插入C表;

2、可以采用对象模型,对其A、B表结构类型,可以采用Linq合并

萧從军 | 园豆:210 (菜鸟二级) | 2011-04-02 10:02
0

dataTable1.Merge(dataTable2);

麒麟.NET | 园豆:3614 (老鸟四级) | 2011-04-02 10:17
0

select  name,sum(count)  from
(
select t.name , t.count from a t where name in (select name from b)
union all
select name , count from b
union all
select name ,count   from a where name not in (select name from b)
) m
group by m.name

wlz0815 | 园豆:587 (小虾三级) | 2011-04-02 13:56
0
select name,sum(count) from
(
select t.name , t.count from a t where name in (select name from b)
union all
select name , count from b
union all
select name ,count from a where name not in (select name from b)
) m
group by m.name
.NET快速开发框架 | 园豆:946 (小虾三级) | 2011-04-02 16:03
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册