LINQ到实体尝试分析不平等比较的列时不能识别方法'的Int32解析(System.String)“方法 [英] LINQ to Entities does not recognize the method 'Int32 Parse(System.String)' method when attempting to parse a column for inequality comparisons
问题描述
我在我的网页下面的代码:
I have following code in my page:
var myVar= Entity.SetName
.Where(p => int.Parse(p.ID) >= start &&
int.Parse(p.ID) <= end);
开始和结束都是整型,但 p.ID 的是字符串。所以我应该转换的 p.ID 的为int。但我得到以下错误:
start and end are int, but p.ID is string. So i should convert p.ID to int. But i get following error:
LINQ到实体无法识别方法的Int32
解析(System.String)方法,这种方法不能被翻译
到店的表情。
LINQ to Entities does not recognize the method 'Int32 Parse(System.String)' method, and this method cannot be translated into a store expression.
问题出在哪里?
推荐答案
的首先,我会强烈建议检查你的数据库设计,是否有一个很好的理由 ID
是一个字符串
。我会考虑改变 ID
DB类型 INT
,您将得到与转换摆脱这个问题的
First, I would highly recommend to check your database design, whether there is a really good reason for ID
to be a string
. I would consider changing the ID
DB type to int
and you will get rid of this problem with converting.
你的手段,即EF不知道如何转换方法的误差 Int32.Parse()
为SQL。
基本上,你有两个选择如何处理的:
The error you get means, that EF does not know how to convert the method Int32.Parse()
to SQL.
Basically you have two options how to deal with that:
执行LINQ外的比较实体:
Do the comparison outside the linq to entities:
var myVar= Entity.SetName.AsEnumerable()
.Where(p => int.Parse(p.ID) >= start &&
int.Parse(p.ID) <= end);
但是,这是的不建议,因为你正在阅读的整个结果从集DB,应用其中,
条件之前。
But this is not recommended, because you are reading whole result set from DB, before applying the where
condition.
或者让自定义模型定义的函数作为
的字符串转换在EF为Int 4.0
或
的实体框架:我在哪里可以延长CSDL / MSL
这篇关于LINQ到实体尝试分析不平等比较的列时不能识别方法'的Int32解析(System.String)“方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!