How to convert Data Type of Columns using LINQ in a DataTable?

There is a Data Table in which the numerical values are in string data format. I want to convert the data type of those multiple column from string to double using LINQ.

you can try use this

1 Like

Thanks for the help.
But I need LINQ query

You can try this in an invoke code, of course with in and out arguments. Here the requested data is in column 3:

'Create a copy of the original table
Dim TableDummy As DataTable=in_CODE_Table.Clone
'Run a LINQ-query with grouping and load a datarow in order to return a datatable
out_CODE_Result=(From row In in_CODE_Table.AsEnumerable()
Select TableDummy.LoadDataRow(New Object() {row.item(0).ToString, row.item(1).ToString, row.item(2).ToString, Convert.ToDouble(row.Item(3).ToString)},False)).CopyToDataTable
1 Like

Hello Speziman,

This linq is good but it does not work to convert a decimal column to a string type column.
I tried to use it but i am getting the following error.

Can you tell me if its still possible to convert?

Thanks,
Yogendra

Hey @kumar.varun2 first create a empty datatable and clone the orginal to it.
Then use this linq query
(from row in dt.asenumerable()
select dt1.rows.LoadDatarow(New object(){convert.todouble(row.field(of object)(columnname))},true)).copytodatatable

Regards

Sreejith S S

Hi Yogendra,

what I read from the error is that you try to convert a string to a decimal. So why are you doing this?
Also the naming of your columns indicates, that these are already strings. Can you provide a bit more information?

Speziman

Hello,

Infact I retreive this data from a SQL database (SqlDT) using the database activities and then try to merge the data of each transaction into a datatable (FinalDT) and write the data in one google sheet.
The problem is when I get data from SQL the datatype of columns is decimal and when I try to merge it in the final datatable the datatype of columns in that is string.
So the merging fails with conflicting properties.

Thats why I wanted to change the column datatype in SqlDT to string and then merge it with FinalDT.

Hope the explanation is clear :slight_smile: