小编Aks*_*Joy的帖子

带有 Linq 表达式的 EF Core SQL 函数 LIKE 方法不适用于非字符串类型

能够使用 SQL 函数创建调用表达式,如下所示

var likeMethod = typeof(DbFunctionsExtensions).GetMethod("Like", new[] { typeof(DbFunctions), typeof(string), typeof(string) });

Expression.Call(null, likeMethod, Expression.Constant(EF.Functions), searchKeyExpression, Expression.Constant($"%{filter.Value}%"));
Run Code Online (Sandbox Code Playgroud)

我只需要了解如何使用整数或十进制列等列的功能与 Like 函数一起使用。如果我使用上述表达式,则会出现以下错误。我怎样才能像使用非字符串数据类型一样使用带有 ef 的表达式

参数例外:System.Int32 表达式中的参数例外不能用于类型 System.String 的方法 Boolean Like (Ef.DBfuntions) 的参数

重现步骤

var likeMethod = typeof(DbFunctionsExtensions).GetMethod("Like", new[] { typeof(DbFunctions), typeof(string), typeof(string) });

Expression.Call(null, likeMethod, Expression.Constant(EF.Functions), searchKeyExpression, Expression.Constant($"%{filter.Value}%"));
Run Code Online (Sandbox Code Playgroud)

正如我所看到的,在下面的示例中,有一个选项可以在 Ef.Functions Like 方法中执行此操作

context.Set<MyEntity>().Where(e => EF.Functions.Like((string)(object)e.IntCol, "%1%"))

Run Code Online (Sandbox Code Playgroud)

但是我如何使用成员表达式来做到这一点。

来源:- https://github.com/aspnet/EntityFrameworkCore/issues/9578

这是直线查询的解决方案。 https://github.com/aspnet/EntityFrameworkCore/issues/16195

更多技术细节

EF Core 版本:(ASP.NET Core 2.1)数据库提供程序:(例如 Microsoft.EntityFrameworkCore.SqlServer) 操作系统:IDE:(例如 Visual Studio 2017 15.4)

c# expression-trees entity-framework-core ef-core-2.1

4
推荐指数
1
解决办法
4281
查看次数