1
var query = from d in db.mytable
         where d.Code == int.Parse(WebUtils.GetQueryString("Code"))
         orderby d.PersonInfo
         select d;

i get the following error

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

i have tried to rewrite the int.Parse(WebUtils.GetQueryString("Code")) like this int intCode = int.TryParse(WebUtils.GetQueryString("OfferCode"));

but still got an error

fofonik
  • 35
  • 6
  • Possible duplicate of [LINQ to Entities does not recognize the method 'Int32 Parse(System.String)' method, and this method cannot be translated into a store expression](http://stackoverflow.com/questions/23210526/linq-to-entities-does-not-recognize-the-method-int32-parsesystem-string-meth) – wkl Apr 12 '17 at 12:43

2 Answers2

1

Just parse your query string outside of the query context:

int code = int.Parse(WebUtils.GetQueryString("Code"));

var query = from d in db.mytable
         where d.Code == code
         orderby d.PersonInfo
         select d;
MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
  • I did that and i got the following error - Input string was not in a correct format. i am using EF 6.1 – fofonik Jul 02 '15 at 18:56
0

You can convert the 'Code' field to string instead of the query string to int. This should works:

var queryString = WebUtils.GetQueryString("Code");
var query = from d in db.mytable
     where SqlFunctions.StringConvert(d.Code) == queryString 
     orderby d.PersonInfo
     select d;

There is no way to convert the string to int. EF doesn't know how to translate that to SQL if you use int.Parse

Arnel
  • 332
  • 2
  • 15
  • i did that. thanks. now i get another error - LINQ to Entities does not recognize the method 'System.String GetQueryString(System.String)' method, and this method cannot be translated into a store expression. any thoughts on that? – fofonik Jul 02 '15 at 19:46
  • thanks a lot. that works. may i ask something else. i have a similar problem with this bit of code var query = from d in db.mytable select new { d.Code, EditModeratedComments = "change" }; Unable to cast the type 'System.Int32' to type 'System.Object'. LINQ to Entities only supports casting EDM primitive or enumeration types. thanks – fofonik Jul 02 '15 at 21:01