我正在使用实体框架,并且我有一行代码正在获取一个 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 to Entities 无法识别方法 'Int32 Parse(System.String)' 方法,并且该方法无法转换为存储表达式."
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 to Entities 无法识别方法 'Int32 ToInt32(System.String)' 方法,并且此方法无法转换为存储表达式
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));
我在其中收到此错误.如您所见,我尝试了几种不同的方法,但没有任何效果,所以任何帮助都会很棒.
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 to Entity
中,您应该使用 query
中支持的方法code>provider 将它们转换为 expression tree
以在您的 Data Base
端运行.
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.
默认情况下,所有提供程序都必须支持某些称为 Canonical Functions
的方法(在这里阅读更多),您也可以将用户定义函数
和存储过程
定义为edm函数
用于 linq 查询
(在这里阅读更多)和(在这里).
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).
此外,您可以使用提供程序支持的方法,并且可以转换为 表达式树library/system.data.objects.entityfunctions%28v=vs.110%29.aspx">EntityFunctions 和 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.
最后关于您的问题,您可以在查询之前转换 UserID
和 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 to Entities 无法识别方法“Int32 Parse(System.String)"方法,并且该方法无法转换为存储表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!