LINQ Query to change the datatype of few columns of a datatable

i have table of data as below
The image shows a spreadsheet with six columns labeled A to F, containing a mix of text and numbers. (Captioned by AI)
i need to change the datatype of column C,E and F to ineteger type, kindly help with a LINQ query for the same

@Amrutha.mg,

If you are looking for changing the format in excel, use Format Cells activity.

i want to change in datatable

Hi,

  1. For Each Row:
  • Input: β€˜YourDataTable’
  1. Assign Activity for Column C:
  • β€˜row(β€œC”) = If(IsNumeric(row(β€œC”).ToString), Convert.ToInt32(row(β€œC”)), 0)’
  1. Assign Activity for Column E:
  • β€˜row(β€œE”) = If(IsNumeric(row(β€œE”).ToString), Convert.ToInt32(row(β€œE”)), 0)’
  1. Assign Activity for Column F:
  • β€˜row(β€œF”) = If(IsNumeric(row(β€œF”).ToString), Convert.ToInt32(row(β€œF”)), 0)’

This will iterate over each row in the β€˜DataTable’ and convert the values in columns β€˜C’, β€˜E’, and β€˜F’ to integers. If the value cannot be converted, it will assign to β€˜0’ as default.

This is probably the shortest and the most readable way to achieve your goal.

You can basically use this query:

DT_Table = DT_Table.AsEnumerable().Select(Function(row)
row(β€œA”) = row(β€œA”).ToString() ’ Convert column A to String
row(β€œB”) = row(β€œB”).ToString() ’ Convert column B to String
row(β€œD”) = row(β€œD”).ToString() ’ Convert column D to String
row(β€œC”) = If(IsNumeric(row(β€œC”).ToString()), Convert.ToInt32(row(β€œC”)), 0) ’ Convert column C to Integer
row(β€œE”) = If(IsNumeric(row(β€œE”).ToString()), Convert.ToInt32(row(β€œE”)), 0) ’ Convert column E to Integer
row(β€œF”) = If(IsNumeric(row(β€œF”).ToString()), Convert.ToInt32(row(β€œF”)), 0) ’ Convert column F to Integer
row
).CopyToDataTable()

Regards,

Kardelen Cihangir

1 Like

@Amrutha.mg
keep in mind

  • when datatable C column is defining the used DataType: e.g. String
  • then: we cannot convert the string into int32 and assigning it back

In such case we do the conversion with a helper column like

  • Add helper Col: C_asInt, ColumnDatatype: Int32
  • Convert C to int - e.g. CInt(row(β€œC”).toString.Trim) and assign it to C_asInt Col of the same row
  • afterwards
    • remove C Column
    • rename: the helper Column to β€œC” (or the other origin column name)
    • reorder columns to the origin orders

Thank you for the Response, i don’t want 0 if the field is blank. if the value is blank previously i want it blank after conversion as well. Kindly respond.

Hello @Amrutha.mg

So suppose you data table is dt_data here is the LinQ query
Use Assign Activity
dt_data = dt_data.AsEnumerable().Select(Function(x) dt_data.Rows.Add _
({x(β€œA”), x(β€œB”), If(String.IsNullOrEmpty(x(β€œC”).ToString), Nothing, CInt(x(β€œC”))), _
x(β€œD”), If(String.IsNullOrEmpty(x(β€œE”).ToString), Nothing, CInt(x(β€œE”))), _
If(String.IsNullOrEmpty(x(β€œF”).ToString), Nothing, CInt(x(β€œF”)))})).CopyToDataTable()

Hope this helps!

Hi thank you for your response but this is throwing me following error
Assign: Collection was modified; enumeration operation might not execute.

Oh sorry!
First clone the datatable use assign declare a new data table variable dt_result
dt_result = dt_data.clone()
And then use this
dt_result = dt_data.AsEnumerable().Select(Function(x) dt_result.Rows.Add _
({x(β€œA”).ToString, x(β€œB”).ToString, If(String.IsNullOrEmpty(x(β€œC”).ToString), Nothing, CInt(x(β€œC”))), _
x(β€œD”).ToString, If(String.IsNullOrEmpty(x(β€œE”).ToString), Nothing, CInt(x(β€œE”))), _
If(String.IsNullOrEmpty(x(β€œF”).ToString), Nothing, CInt(x(β€œF”)))})).CopyToDataTable()

Try this !