How to convert string to int for SQL?
Brand spanking new to LINQ, enjoy it so far but having hard time with what seems like simple task. Tried googling all different types with no success. Tried int.Parse, Convert.ToInt32,
Converting to C# Programs (with this have no idea how to point to my table)
Thanks for any help
Converting to C# Programs (with this have no idea how to point to my table)
Thanks for any help
from p in ProviderInfo where "056428" == p.Provnum //where 056428 == int.Parse( p.Provnum ) // Method 'Int32 Parse(System.String)' has no supported translation to SQL. //where 056428 == Convert.ToInt32( p.Provnum ) // SqlException: Conversion failed when converting the nvarchar value '37E024' to data type int. select new{ p.Provnum ,p.PROVNAME ,p.PHONE }
Comments
I don't think Linq2Sql supports isNumeric, so you can either :
fix your table structure and your data,
use TSQL and IsNumeric or
try to use not like to exclude the non-numeric data.
For example, to exclude using a not like you could try The . and , are included in case you have values like "123,456" or "123.", but this can also return values like 123,456.78 and may throw errors with values like ",," , so this isn't really recommended if you can not control the sql data.You can also map out the sql functions that you want to use in your query that are not directly supported. You don't have to implement them at all, it would be translated to the appropriate sql calls.
Since your linqpad script code is actually inserted into the data context code, you could just add this anywhere in your script. Just make sure you use
C# Program
for your language.One minor issue
seems to try the convert before the IsNumeric test and hence can throw the Sql Conversion error.
It should be or
where IsNumeric(p.Provnum) == 1
Linq does not recognize IsNumeric