How to sum the column in datatable by using Linq Query

Hi Guys,

I have a datatable :-

Incident Type Incident Cost
Burn $3,367
Vehicle $132
Cut $4,872
Falling object $1,248
Lifting $29
Crush & Pinch $2,525
Crush & Pinch $59
Burn $1,947
Fall $1,248

I wanna get the total amount of Incident Cost by linq or Select query.

Please help me

QuickDirty ( we do not like number cleansing on string method base)

myTotal | Double =
yourDataTableVar.AsEnumerable.Sum(Function (x) CDbl(x(1).toString.Trim.Replace("$","")))

Hi @Dmitri00007 ,

Could you try this and see if it works out for you?

dt_result.AsEnumerable().Sum(Function(row) Convert.ToDouble(System.Text.RegularExpressions.Regex.Replace(row("Incident Cost").ToString,"\D","")))

Kind Regards,
Ashwin A.K

Assign: Input string was not in a correct format.

This is the error, uipath is showing

Assign: Conversion from string “” to type ‘Double’ is not valid.

This is the error, uipath is showing

Hi @Dmitri00007 ,

Could you verify whether the variable you are assigning the value to is of type System.Double?

Kind Regards,
Ashwin A.K

yes its System.Double

HI @Dmitri00007

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

1 Like

its working

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.