5

am able to create an invoke Expression with SQL Functions like as shown below

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}%"));

I just need to understand how can I use the functionality for columns like integer or decimal column to use with Like functions. If I use the above expression I am getting below error. How can I use expression with ef like with non string datatype

Argument Exeption: argument exception in Expression of System.Int32 can not be used for parameter for type System.String of method Boolean Like (Ef.DBfuntions)

Steps to reproduce

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}%"));

As I can see there is an option for to do it in Ef.Functions Like method in the below example

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

but how can I do this using Member Expressions.

Source :- https://github.com/aspnet/EntityFrameworkCore/issues/9578

This is the solution for direct line query. https://github.com/aspnet/EntityFrameworkCore/issues/16195

Further technical details

EF Core version: (ASP.NET Core 2.1) Database Provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer) Operating system: IDE: (e.g. Visual Studio 2017 15.4)

7
  • Imagine you were writing this directly in SQL. What exact SQL are you trying to generate / emulate? Are you sure that SQL actually works (i.e. did you try to run it against the database)?
    – mjwills
    Jun 22, 2019 at 12:34
  • It works for string , for EF Functions extension has method for two string parameters. But I want to use functionality for number types. context.Set<MyEntity>().Where(e => EF.Functions.Like((string)(object)e.IntCol, "%1%")). This converts SQL statement with like for Integer columns.this is what I found for direct function. I am looking for same with member expression
    – Akshay Joy
    Jun 22, 2019 at 13:56
  • I have updated the logic for like operations with expression link. But same thing how can I use for example integer, decimal, bigint colums
    – Akshay Joy
    Jun 22, 2019 at 14:01
  • It makes sense , Otherwise I won't post it here.
    – Akshay Joy
    Jun 22, 2019 at 14:08
  • 2
    For the people who commented without understanding my question, please check Ivan Stoev answer. If you don't understand the question, you can just ignore. I posted this question because Answer is out there, I was stuck with conversion part. Which he explained nicely.
    – Akshay Joy
    Jun 23, 2019 at 7:06

1 Answer 1

11

The "double cast" (string)(object)e.IntCol is a way to trick the C# compiler to "pass" int argument to a method expecting string parameter (like EF.Functions.Like). Of course if the method is actually called, you'll get invalid cast exception at runtime.

But the trick works because methods like this are never "called", but translated to SQL, and SqlServer EF Core provider removes such casts and allows you to use the SqlServer implicit data conversions. I'm using the same technique (although in opposite direction) in How can a JSON_VALUE be converted to a DateTime with EF Core 2.2? and Expression tree to SQL with EF Core.

Here is how that maps to Expression methods. Given Expression searchKeyExpression (the concrete Expression type doesn't matter), the important is the Type returned by the Expression.Type property. If it is string, you are fine, otherwise you need to apply (string)(object) casts to it, which is achieved with two Expression.Convert calls.

Something like this:

Expression matchExpression = searchKeyExpression;
if (matchExpression.Type != typeof(string))
{
    matchExpression = Expression.Convert(matchExpression, typeof(object));
    matchExpression = Expression.Convert(matchExpression, typeof(string));
}
var pattern = Expression.Constant($"%{filter.Value}%");
var callLike = Expression.Call(
    typeof(DbFunctionsExtensions), "Like", Type.EmptyTypes,
    Expression.Constant(EF.Functions), matchExpression, pattern);
4
  • That was brilliant trick. Happy that I am not the only faced this situation. Actually Ity almost my sleeping time. Will check and let you know as mark as answer. I was stuck at the conversion part. But one question u mentioned Type.EmptyTypes I didn't get that part
    – Akshay Joy
    Jun 22, 2019 at 21:07
  • 2
    This is a handy overload of Expression.Call for "calling" static methods. The 3rd argument is Type[] representing the generic type arguments of the method. For non generic methods we simply pass empty type array, what exactly the Type.EmptyTypes is :) Of course your way (using another Call overload) also works when you pass the correct arguments.
    – Ivan Stoev
    Jun 23, 2019 at 0:52
  • We were using the same fix in EF Core 2.2, but it now no longer works in EF Core 3.*. The exception message is "Object reference not set to an instance of an object." Stacktrace: at Microsoft.EntityFrameworkCore.Query.ExpressionExtensions.InferTypeMapping(SqlExpression[] expressions) at Microsoft.EntityFrameworkCore.Query.SqlExpressionFactory.ApplyTypeMappingOnLike(LikeExpression likeExpression) at Microsoft.EntityFrameworkCore.Query.SqlExpressionFactory.ApplyTypeMapping(SqlExpression sqlExpression, RelationalTypeMapping typeMapping) Dec 20, 2019 at 9:45
  • 1
    Updating Microsoft.EntityFrameworkCore and Microsoft.EntityFrameworkCore.SqlServer to 3.1.0 fixes this issue Dec 29, 2019 at 6:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.