Format a Data Table column using LINQ

I have a DataTable with a column containing numbers. I am formatting it in 0.00 format using the LINQ query below, but it is showing an error. Can anyone help me?

DT1.AsEnumerable().Select(Function(row)
row.SetField(“Number”, String.Format(“{0:0.00}”, Convert.ToDouble(row(“Number”))))
Return row
End Function).CopyToDataTable()

1 Like

can you try this and let me know if it works
DT1.AsEnumerable().ToList().ForEach(Sub(row)
row.SetField(“Number”, String.Format(“{0:0.00}”, Convert.ToDouble(row(“Number”))))
End Sub)

3 Likes

Hello,
The SetFiled is causing the issue in your LINQ query , It modifies the row but it doent return anything. Please try the code which @chandreshsinh.jadeja has sent, it should work, and if you still face any issue then please let us know the errror msg .

2 Likes

Hi @Lalit_Chaudhari

You can use the below LINQ Expression,

DT1.AsEnumerable().Select(Function(row)
    row.SetField("Number", String.Format("{0:0.00}", Convert.ToDouble(row("Number").ToString())))
    Return row
End Function).CopyToDataTable()

You might be using the expression in Assign activity, but you have to use it in the Invoke code activity.

Hope it helps!!

1 Like

if that query works then please dont forget to mark it as answer

1 Like