Using variable instead of column name - to sum

Continuing the discussion from Sum the values in the column:

Would like to know if i can use variables in the place of column name.

dt3.AsEnumerable.Sum(Function(x) Convert.ToDouble(x(“columnname”).ToString.Trim) )

dt2.AsEnumerable.Sum(Function(x) Convert.ToDouble(x("(Bill “+numBill.ToString+”)").ToString.Trim) )

As the 2nd piece of code isn’t working

Thank you xoxo

HI @sangasangasanga,

dt2.AsEnumerable.Sum(Function(x) Convert.ToDouble(x(""+numBill.ToString+"").ToString.Trim))

what exception are you getting?

Regards,
Arivu

Convert.ToDouble(x(numBill.ToString)) should work

Hi @arivu96, it not working.

This is my code:
dt2.AsEnumerable.Sum(Function(x) Convert.ToDouble(x("(Bill “+numBill.ToString+”)").ToString.Trim))

FYI: numbill is a variable with generic value type

Pls help thank you.

Hi @Uemoe , where do I add it in?

If i add it in the below code, gives me validation error.

Hi, please help if this is possible.

There could possibly be a value that is not a number in that column. You probably want to check that the value is a number with inline If condition.

dt2.AsEnumerable.Sum(Function(x) If(isNumeric(x("(Bill “+numBill.ToString+”)").ToString.Trim), Convert.ToDouble(x("(Bill “+numBill.ToString+”)").ToString.Trim), 0))

So if value is not a number, it will use a zero.