tags:

views:

42

answers:

1

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 columns in a DataTable.

Thank you.

A: 

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.

LBushkin
Thank you for the answer! My application runs on .net 2.0 :( Will dictionary be a big overhead? It will contain at least 5k elements.
šljaker
Dictionaries scale fairly well, so long as the key that you're using has a good distribution of hashvalues. It will also be a much faster implementation than other algorithsm (such as nested loops).
LBushkin