Sum values in a DataTable by a given criteria
Which is the most effective way to sum data in the DataTable by a given criteria?
I have the following table:
KEY_1
KEY_2,
VALUE_1,
VALUE_2
Input:
01101, P, 2, 3
01101, F, 1, 1
01101, P, 4, 4
10102, F, 5, 7
Desired output (new DataTable):
01101, P, 6, 7
01101, F, 1, 1
01101, SUM, 7, 8
10102, F, 5, 7
10102, SUM, 5, 7
I need efficient algorithm, because I have 10k rows and 18开发者_JAVA百科 columns in a DataTable.
Thank you.
You may want to consider using LINQ - it has a GroupBy operator which may allows you to do this easily with relatively good performance given teh data size you describe.
If you need a super-optimized implementation, then you may want to create your own intermediate data structure to represent a summed record, store that some kind of dictionary based on the key, and update it as you loop over each record.
精彩评论