Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
204 views
in Technique[技术] by (71.8m points)

How to merge multiple DataTable objects with group by with C# and concatinating duplicate rows?

Hi I have following scenario:

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

Table 1:

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

Table 2:

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

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?

I use C# 2.0

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...