LINQ到实体无​​法识别方法“的Int32解析(System.String)”的方法,而这种方法不能被翻译成店前pression [英] LINQ to Entities does not recognize the method 'Int32 Parse(System.String)' method, and this method cannot be translated into a store expression

查看:442
本文介绍了LINQ到实体无​​法识别方法“的Int32解析(System.String)”的方法,而这种方法不能被翻译成店前pression的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用实体框架,我有一条线code,它走的是一条VAR和翻译回数据库的IINT的。

I am using Entity Framework, and I have a line of code that is taking a var and translating it back to an iint for the database.

var record = context.enrollments.SingleOrDefault
  (row => row.userId == int.Parse(UserID) && row.classId == int.Parse(ClassID));

每当我尝试运行它,我收到rhis错误。 LINQ到实体无​​法识别方法的Int32解析(System.String)的方法,而这种方法不能被翻译成店前pression。

Whenever I try to run it I receive rhis error. "LINQ to Entities does not recognize the method 'Int32 Parse(System.String)' method, and this method cannot be translated into a store expression."

我已经试过此以及

 var record = context.enrollments.FirstOrDefault
  (row => row.userId == Convert.ToInt32(UserID) 
  && row.classId == Convert.ToInt32(ClassID));

和所有我是收到此错误信息,LINQ到实体无​​法识别方法的Int32 ToInt32(System.String)的方法,而这种方法不能被翻译成店前pression

and all I receive is this error message, "LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method, and this method cannot be translated into a store expression

最后我都试过这个为好,这我​​知道是不寻常的,但它在过去已经工作了类似的情况。

and finally I have tried this as well, which I know is unusual, but it has worked in the past for similar situations.

var record = context.enrollments.SingleOrDefault
  (row => row.userId == CommonDLL.Sanitize<int>.ConvertType(UserID) 
  && row.classId == CommonDLL.Sanitize<int>.ConvertType(ClassID));

在我得到这个错误。正如你可以看到我已经试过seveal不同的东西,并没有什么工作,所以任何帮助将是巨大的。

In which I get this error. As you can see I have tried seveal different things and nothing is working, so any help would be great.

推荐答案

LINQ到实体,你应该用你的查询方法这是由你的提供商将它们转换为前pression树支持运行在数据库的一面。

in Linq to Entity, you should use the methods in your query which is supported by your provider to convert them to expression tree to run on your Data Base side.

所有供应商必须支持默认调用一些方法规范函数Read更多这里),也可以定义你的用户自定义函数存储过程 EDM功能 LINQ查询使用Read更多在这里)和(的Here )。

all providers must support some methods by default called Canonical Functions (Read More Here), and also you can define your user defined function and stored procedure as edm functions to use in linq query (Read More Here) and (Here).

除了可以使用由供应商支持,并可以转换为前pression树这是在<一个方法href=\"http://msdn.microsoft.com/en-us/library/system.data.objects.entityfunctions%28v=vs.110%29.aspx\">EntityFunctions和<一个href=\"http://msdn.microsoft.com/en-us/library/system.data.objects.sqlclient.sqlfunctions%28v=vs.110%29.aspx\">SqlFunctions.

in addition you can use methods which is supported by providers and can be converted to expression tree which are in EntityFunctions and SqlFunctions.

和最后关于你的问题,你可以转换用户名的ClassID 之前,您的查询,如:

and finally about your question, you can convert UserID and ClassID before your query, like this:

var UID = int.Parse(UserID);
var CID = int.Parse(ClassID);
var record = context.enrollments.SingleOrDefault
    (row => row.userId == UID && row.classId == CID);

这篇关于LINQ到实体无​​法识别方法“的Int32解析(System.String)”的方法,而这种方法不能被翻译成店前pression的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆