问题描述
我尝试使用LINQ查询时,当我尝试以某种方式转换高度值时,我在运行时会遇到错误.
错误:
linq到实体无法识别方法'int32 toint32(System.String)'方法,无法翻译此方法 进入商店表达式.
有更好的方法比较高度值吗?
case "HeightFrom": photosquery = photosquery.Where(x => Convert.ToInt32(x.physical.BodyHeight.TrimEnd()) >= Convert.ToInt32(height)); break;
推荐答案
问题是您使用的LINQ提供商(可能来自EF)不支持Convert.ToInt32.解决此问题的一个快速解决方案是使用AsEnumerable扩展方法切换到Linq到对象
photosquery = photosquery.AsEnumerale() .Where(x => Convert.ToInt32(x.physical.BodyHeight.TrimEnd()) >= Convert.ToInt32(height));
现在,如果BodyHeight是一个字符串,并且您要保存整数,我强烈建议如果可以的话,请更改该列的类型.您的查询可以完全在服务器端执行:
int value=Convert.ToInt32(height); photosquery = photosquery.Where(x => x.physical.BodyHeight >= value);
其他推荐答案
case "HeightFrom": var h = Convert.ToInt32(height); photosquery = photosquery.Where(x => Convert.ToInt32(x.physical.BodyHeight.TrimEnd()) >= h); break;
ef不会知道如何翻译最后一个转换.如果第一个不起作用,只需尝试int.parse(x.physical.BodyHeight.TrimEnd())或直接铸造(int)x.physical.BodyHeight.TrimEnd()
如果是我,并且有时间,我可能会在开始查询之前将该模型映射到具有正确数据类型的另一个模型.
其他推荐答案
如果您的数据不多,您可以使用
case "HeightFrom": photosquery = photosquery.ToList().Where(x => Convert.ToInt32(x.physical.BodyHeight.TrimEnd()) >= Convert.ToInt32(height)); break;
但是,对于重数据不正确,首先加载所有数据,然后过滤
问题描述
I'm trying to use the linq query, when I try to convert the height value somehow I'm getting error during runtime.
Error:
LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method, and this method cannot be translated into a store expression.
Is there a better way to compare the height value?
case "HeightFrom": photosquery = photosquery.Where(x => Convert.ToInt32(x.physical.BodyHeight.TrimEnd()) >= Convert.ToInt32(height)); break;
推荐答案
The issue is the Linq provider (probably from EF) you are using doesn't support Convert.ToInt32. A quick solution to this issue is using AsEnumerable extension method to switch to Linq to Objects
photosquery = photosquery.AsEnumerale() .Where(x => Convert.ToInt32(x.physical.BodyHeight.TrimEnd()) >= Convert.ToInt32(height));
Now if BodyHeight is a string and you are saving an integer, I strongly suggest to change the type of that column if you can. Your query could be executed entirely in the server side:
int value=Convert.ToInt32(height); photosquery = photosquery.Where(x => x.physical.BodyHeight >= value);
其他推荐答案
case "HeightFrom": var h = Convert.ToInt32(height); photosquery = photosquery.Where(x => Convert.ToInt32(x.physical.BodyHeight.TrimEnd()) >= h); break;
EF won't know how to translate that last convert. If the first one doesn't work, just try int.parse(x.physical.BodyHeight.TrimEnd()) or a direct cast (int)x.physical.BodyHeight.TrimEnd()
If it were me, and I had the time, I would probably map that model to another model with the correct data types before I started querying.
其他推荐答案
If your data is not a lot you can use
case "HeightFrom": photosquery = photosquery.ToList().Where(x => Convert.ToInt32(x.physical.BodyHeight.TrimEnd()) >= Convert.ToInt32(height)); break;
But for heavy data is not ok and load all data at first then filter it