i have table of data as below

i need to change the datatype of column C,E and F to ineteger type, kindly help with a LINQ query for the same
If you are looking for changing the format in excel, use Format Cells activity.
i want to change in datatable
Hi,
- For Each Row:
- Input: βYourDataTableβ
- Assign Activity for Column C:
- βrow(βCβ) = If(IsNumeric(row(βCβ).ToString), Convert.ToInt32(row(βCβ)), 0)β
- Assign Activity for Column E:
- βrow(βEβ) = If(IsNumeric(row(βEβ).ToString), Convert.ToInt32(row(βEβ)), 0)β
- 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
@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 !