Hi ,
I need to sum a column in a data table based on column index.
for exmaple
In the above image it should sum based on column index instead of column Name.
Hi ,
I need to sum a column in a data table based on column index.
for exmaple
In the above image it should sum based on column index instead of column Name.
Suppose your Datatable is Dt1 then you can write something like,
DT1.AsEnumerable().Sum(Function(x) Cdbl(x(1))) , here 1 is the column index that you can choose accordingly.
Thanks
Welcome back to community
Try this expression
(From d in dtData.AsEnumerable Where Not (isNothing(d(1)) OrElse String.IsNullorEmpty(d(1).toString.Trim)) Select v = CDbl(d(1).toString.Trim)).Sum(Function (x) x)
Regards
Gokul
Hi @karthik_kulkarni1 ,
Could you try this?
Dt_sampleData.AsEnumerable().Where(Function(w) Not(Nothing(w(0)) OrElse String.IsNullOrEmpty(w(0).Tostring))).Sum(Function(s) Convert.ToDouble(s(0).ToString))
Kind Regards,
Ashwin A.K
Hi everyone,
As it is a csv file so every time when I am index it is throwing error as column 1 is not available.
Hi @karthik_kulkarni1 ,
Does the DataTable consist of a single column?
If that is the case, then try with row(0) or you can pass in its column name instead.
Let us know if you face any issues, also screenshots of the DataTable would be most appreciated.
Kind Regards,
Ashwin A.K
Hi Ashok,
My datatbale is as below ,and I need to sum for yellow marked column
Could you please help me with the function to take.
Hi @karthik_kulkarni1 ,
That looks like column 9, correct?
If you are comfortable using indexes, then this ought to work:
Convert.ToDouble(Dt_sampleData.AsEnumerable().Where(Function(w) Not(IsNothing(w(8)) OrElse String.IsNullOrEmpty(w(8).Tostring))).Sum(Function(s) Convert.ToDouble(s(8).ToString)))
Kind Regards,
Ashwin A.K
Try this expression
(From d In DtMaster.AsEnumerable Where Not (isNothing(d(8)) OrElse String.IsNullorEmpty(d(8).toString.Trim)) Select v = CDbl(d(8).toString.Trim)).Sum(Function (x) x)
Regards
Gokul
try this →
Outdt.AsEnumerable.Sum(Function(x) Cint(x(0)))
Outdt-> Your datatable .
X(0)-> 0 represents column index.
Hi @ashwin.ashok ,
I have tried using your code it is executing properly on my local machine,but when I am running the same code on server getting error as specified column not available.
Is that something with CSV format issue?
I mean deleimeter.