How to calculate sum of a DataTable's Column in LINQ (to Dataset)?
If untyped (replace int
with the correct data type):
var sum = table.AsEnumerable().Sum(x=>x.Field<int>(3));
or:
var sum = table.AsEnumerable().Sum(x=>x.Field<int>("SomeProperty"));
If typed:
var sum = table.Sum(x=>x.SomeProperty);
If you data field is integer
var sum = TableData.Sum(x => x.FieldName);
If your data field is string then you need to parse it as integer
var sum = TableData.Sum(x => Int32.Parse(x.FieldName));
If your data field is string and you want to store result as string
var sum = TableData.Sum(x => Int32.Parse(x.FieldName)).ToString();