0

I have a task to get data from database and draw a diagram based on it. The problem is the data that I need is stored as string (but it should be decimal, why they did it is a mystery and they are not going to make it decimal and that is for sure). Here is the LINQ query that I write:

from SPEKTR in db.SPEKTR
               where
               Convert.ToInt32(SPEKTR.RF) > 88 &&
               Convert.ToInt32(SPEKTR.RF) < 90
               select SPEKTR;

And this is the error that I get

{"LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method, and this method cannot be translated into a store expression."}

Are there any options to convert that string?

Atur
  • 1,712
  • 6
  • 32
  • 42
  • My proposed duplicate is one because [`Convert.ToInt32`](http://referencesource.microsoft.com/#mscorlib/system/convert.cs,069d595792a157df) calls `int.Parse`. – Tim Schmelter Feb 02 '15 at 12:18
  • If the table isn't too big, you can load data into memory using `AsEnumerable()` and then use `Convert.ToInt32`. – Yuval Itzchakov Feb 02 '15 at 12:20
  • 1
    @TimSchmelter just a remark : the "duplicate" is about converting the "comparison parameters", not converting the db fields... Which will need a slightly different answer... – Raphaël Althaus Feb 02 '15 at 12:21
  • @YuvalItzchakov data is way too big but SQL server is strong. – Javid Yagubali Feb 02 '15 at 12:33
  • @JavidYagubali: i just added a comment why i've closed it as duplicate even if the other question asks for `int.Parse` instead of `System.Convert.ToInt32` – Tim Schmelter Feb 02 '15 at 12:38
  • @RaphaëlAlthaus: yes, good point. But the accepted answer explains both. – Tim Schmelter Feb 02 '15 at 12:39
  • 1
    @TimSchmelter I read almost all questions about type conversion, none of them had the answer to this question. I think this problem is somehow different, f.y.i., I'm just a noob in microsoft related technologies – Javid Yagubali Feb 02 '15 at 12:43
  • This answer has a solution: http://stackoverflow.com/a/5971677/468973 – Magnus Feb 02 '15 at 12:44
  • I think I got the point, thank you for your effort and patience! – Javid Yagubali Feb 02 '15 at 12:48
  • @Magnus that answer didn't help, i used code-first approach, I don't have any *.edmx file – Javid Yagubali Feb 02 '15 at 12:57
  • i used this code to reproduce your problem: List list = new List {"1.5","2.5","3.1","4.2" }; var a = ( from aa in list where Convert.ToDouble(aa) > 1 select aa ); it worked for me ( notice that i used ToDouble and not Int32 because you said that the values are decimals ) – Liran Feb 02 '15 at 13:57
  • So I created a new class which was exactly the same with my entity class, but the types were what they needed to be. Using a stored procedure I could get database information to a list, and use this list as I got it from entity framework. Basically, just because the guy who has 6 years of experience didn't want to store a decimal as a decimal, we are not able to write a proper code... – Javid Yagubali Feb 05 '15 at 06:59

0 Answers0