DataRow [] C # Aggregate Functions

I have an array of DataRow objects in my C # project that I would like to summarize from different fields.

Instead of running each row and summing my own amounts, I noticed the DataRow [] function. Sum <>, but I'm struggling to find any resources on the network about how to use it.

Any pointers in the right direction will be very helpful.

:) Remote sample code, because it was wrong! Everything now works great - the link helped amuse Mark.

+4
source share
1 answer

This is LINQ Sum ; and can be used:

 var sum = rows.Sum(row => row.Field<int>("SomeColumn")); // use correct data type 

(you can also pass a column index or a DataColumn instead of a row)

for untyped datarows or:

 var sum = rows.Sum(row => row.SomeColumn); 

for typed datarows.

MSDN has full documentation on this subject; for example, here is the overload that I use in the examples above.

+13
source

All Articles