Error converting Int to String with LINQ

I have a data table that is filtered to remove duplicate rows based on the values presented in one column, this is done via LINQ. Here is the error message that is encountered when removing duplicates:

“Unable to cast object of type ‘System.Double’ to type ‘System.String’.”

The LINQ query:
DT.AsEnumerable().GroupBy(Function(r) r.Field(of String)(Column A value).toString).Select(Function(s) s.First()).CopyToDataTable

Note: I’m not using Filter DataTable activity because I’m not aware of a way for it to remove whole rows based on the value in a single column, up for suggestions if it’s possible

There is an error with the LINQ query when it encounters integers in the data table, but it’s expecting strings. Tracing back through the code and looking at values during execution it seems that all works fine when the first value in the required column (Column A in example below) is presented as a string:

Example data (all is ok)
-Column A-
bill
apple
car

But when the first value in the column (Column A in example below) is an integer I get the error stated above.

Example data (error occurs when removing duplicates later)
-Column A-
123
456
bill
apple

I need help making the LINQ query cast the column A value to a string.

Sorry for the long post, I tried to address as many questions as I could think of to save some time on follow ups. Any and all help, tips, tricks are appreciated, thank you!

with the Field method and to String it is double converted, give a try at:

DT.AsEnumerable().GroupBy(Function(r) r("Column A value").toString.ToUpper.Trim).Select(Function(s) s.First()).CopyToDataTable
1 Like

Thank you so much @ppr for the response, it definitely solved the conversion issue. I’ve seen your input throughout the forums and yours and several other active members’ activity and replies are very much appreciated.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.