How to add all the values of a column in Datatable by using Linq (without using for each)

Hi All,

I used the below line of code, but its showing “Expression Expected”
Can anyone help on this

Hi @tgopalas

Use this

MaxValue=Convert.ToInt32(dt.AsEnumerable().Sum(Function(dt.rows(“amount”)))

Thanks
Ashwin.S

2 Likes

Hey @tgopalas,

Use this:

dt.AsEnumerable().Sum(function(x) x.Amount ) OR dt.AsEnumerable().Sum(function(x) x.Field(“Amount”) )

1 Like

Thanks for the quick response @AshwinS2 @amarasto :slight_smile:
Its quite a good learning here with various ways of achieving the same.
I used the below line and its working too …

Convert.ToInt32(dt.Compute(“SUM(Amount)”, String.Empty))

1 Like