如何使用C#将多个DataTable对象与组合并合并重复的行? [英] How to merge multiple DataTable objects with group by with C# and concatinating duplicate rows?

查看:213
本文介绍了如何使用C#将多个DataTable对象与组合并合并重复的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况:

我有一组查询将返回一组DataTable对象。数据表将具有几行相同,少数列是每个表唯一的。例如

I have a set of queries which will return me set of DataTable objects. The datatable will have few rows columns that are identical and few columns that are unique to each table. for example

表1:

Name   | Jan Data  | Feb Data
A      |10         |20
B      |12         |9

2:

Name   | Mar Data  | Apr Data  | May Data
A      |12         |21         |5
C      |14         |90         |6

现在我想合并这两个数据表对象。但是在合并的时候,有一些这样的名字,例如A,这两个表和数据是不同的。所以我想要一个输出如:

Now i want to merge these two data table objects. but while merging there are names such as A which is common to both tables and data varies. so i want an output such as:

Name   | Jan Data  | Feb Data  | Mar Data  | Apr Data  | May Data
A      |12         |21         |5          | 45        |52 
B      | 0         | 0         |           |62         |21
C      |14         |90         |6          |63         |42

当按名称分组时,表将具有每行值的总和。 (我没有在这里表示歉意的麻烦)

the resultant table will have sum of each row value when grouped by name. (i did not take the trouble of calculating here apologies)

你能给我一个方法可以实现吗?

Can you suggest me an approach how this can be achieved?

我使用C#2.0

推荐答案

我会写另一个查询来组合这两个,并正确返回我想要的。你所描述的是关系数据库和SQL是做什么的。我不会在代码中做,除非我绝对不得不。

I'd write another query to combine the two and return exactly what I wanted. What you're describing is what relational databases and SQL are made to do. I wouldn't do it in code unless I absolutely had to.

这篇关于如何使用C#将多个DataTable对象与组合并合并重复的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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