如何将 and 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
编辑
我得到的错误是:
LINQ to Entities 无法识别“System.String ToString()”方法,并且此方法无法转换为存储表达式
最佳答案
使用 EF v4,您可以使用 SqlFunctions.StringConvert 。所以你的代码最终看起来像这样:
from s in ctx.Services
where SqlFunctions.StringConvert((double)s.Code).StartsWith("1")
select s
编辑
正如 Rick 在他的评论中提到的,将 int 转换为 double(十进制也可能有效)的原因是该函数没有 int 的重载。
关于.net - Linq int 到 string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1228318/