c# - 为什么无法在 LinQ 中使用 SqlFunctions 将 int 转换为 string?

标签 c# linq entity-framework sql-server-ce

为什么这个 Linq 不起作用?

Groups.Where(x => SqlFunctions.StringConvert((double?)x.Code)
                              .Trim().StartsWith(txtSearch.Text))
      .ToList(); 

错误

The specified method 'System.String StringConvert(System.Nullable`1[System.Double])' on the type 'System.Data.Entity.SqlServer.SqlFunctions' cannot be translated into a LINQ to Entities store expression.

我正在使用 EF6/SQL CE/C#

Code 是一个 int 字段

最佳答案

您应该使用SqlceFunctions而不是SqlFunctions

所以你的代码应该是

  Groups.Where(x =>  SqlCeFunctions.StringConvert ((double?) x.Code)
                                   .TrimStart() .StartsWith(txtSearch.Text))
        .ToList();

命名空间:System.Data.Entity.SqlServerCompact;

关于c# - 为什么无法在 LinQ 中使用 SqlFunctions 将 int 转换为 string?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20371972/

相关文章:

c# - 更改用户代理字符串

c# - 使用 String.Format() 格式化字符串日期

c# - 如何使用 LINQ 在 ASP.NET Core 中从 ModelState 中获取充满错误消息的 IEnumerable

c# - Automapper Custom Map 使用 "Include"来获取 child 数

c# - SQL Server to .Net Decimals with EF6 database first issue

c# - 我如何确定我的所有线程何时完成执行?

c# - Thread.Yield 是确定多线程应用程序 C# 中是否存在错误的标准方法吗

linq - 基于组合框值构建动态LINQ查询

entity-framework - Entity Framework 4 : which approach is the best

c# - 如何在单元测试中使用 Moq 和 DbFunctions 来防止 NotSupportedException?