LINQ to Entities 无法识别方法 'Int32 Parse(System.String)' 方法

LINQ to Entities does not recognize the method 'Int32 Parse(System.String)' method

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

我收到以下错误

LINQ to Entities 无法识别方法 'Int32 Parse(System.String)' 方法,并且此方法无法转换为存储表达式。

我试过像这样重写 int.Parse(WebUtils.GetQueryString("Code")) int intCode = int.TryParse(WebUtils.GetQueryString("OfferCode"));

但还是报错

只需在查询上下文之外解析您的查询字符串:

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

var query = from d in db.mytable
         where d.Code == code
         orderby d.PersonInfo
         select d;

您可以将 'Code' 字段转换为字符串,而不是将查询字符串转换为 int。 这应该有效:

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

无法将字符串转换为 int。如果您使用 int.Parse

,EF 不知道如何将其转换为 SQL