.net - Linq int 到 string

标签 .net linq linq-to-entities

如何将 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/

相关文章:

c# - 是否可以通过 Dynamic LINQ 进行注入(inject)?

c# - 当文本超出范围 WPF 时显示省略号 (...) 按钮

.net - 为每个用户创建子域

.net - 在 C# Windows 运行时组件中,如何警告开发人员不要在公共(public)类中使用 ==?

c# - Linq 返回具有与单独列表中的所有项目匹配的子项目的父对象

c# - 使用 LINQ ForEach 循环的替代方法是什么?

c# - 当 IDENTITY_INSERT 设置为 OFF 时,无法在表中插入标识列的显式值

c# - Linq 到实体 : Unions + Distinct

c# - 在子对象属性上使用 Sum 的 LINQ to Entities 查询问题

linq - 无法创建常量值 - 仅允许原始类型或枚举类型