pis*_*hio 48 .net linq linq-to-entities
如何将int和int转换为字符串?以下都不起作用:
from s in ctx.Services
where s.Code.ToString().StartsWith("1")
select s
from s in ctx.Services
where Convert.ToString(s.Code).StartsWith("1")
select s
from s in ctx.Services
where ((string)s.Code).ToString().StartsWith("1")
select s
Run Code Online (Sandbox Code Playgroud)
编辑
我得到的错误是:
LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression
Bri*_*hon 55
使用EF v4,您可以使用SqlFunctions.StringConvert.所以你的代码看起来像这样:
from s in ctx.Services
where SqlFunctions.StringConvert((double)s.Code).StartsWith("1")
select s
Run Code Online (Sandbox Code Playgroud)
编辑
正如Rick在他的评论中提到的,将int转换为double(十进制可能也有效)的原因是该函数没有int的重载.
Shu*_*oUk 33
Linq to Entities不支持尽可能多的函数转换为服务器托管的sql.
ToString(在任何东西上)就是其中之一
这是支持的功能列表
之前已经在Stack溢出时询问过这一点,特别是询问如何将int转换为字符串