问题描述:

I need to generate a new Datatable by summing up specific columns for each row from the given datatable.

Below is the example of what I want to achieve.

DataTable1

 1a 1b 1c 2a

1a 0.77 0.17 0.04 0.01

1b 0.08 0.63 0.22 0.04

1c 0.01 0.11 0.61 0.16

2a 0.00 0.02 0.19 0.49

The DataTable2 that I want to generate will have exactly same number of rows & columns with same headers (this I achieve by Clone() method)

However in DataTable2, for each corresponding row from table1

Cell[0][0] should be = sum of 1a+1b+1c+2a

Cell[0][1] = sum of 1b+1c+2a

Cell[0][2] = sum of 1c+2a

and so on...

In short, it should start summing up from current column till the last column for each row.

Is there any easy way to doing this, may be using some inbuilt method from DataTable or may be using linq with minimum amount of code?

I know about Compute() and Expression but not sure if they can be used easily for task.

P.S. -This is just one part of the calculation, once I figure out the way to sum values as per the above logic I want to pass calculated values to another function in my code to convert them to some other number.

相关阅读:
Top