C # expression to return TSQL Contains a keyword

I have the following code:

public Expression FilterString(string property, string Value, ParameterExpression parameter)
{
    var getname = Expression.Property(parameter, property);

    var toLower = Expression.Call(getname, "ToLower", null, null);

    var contains = Expression.Call(toLower, "Contains", null, new[] { Expression.Constant(Value.ToString().ToLower()) });

    //This will result in "LOWER(Body) LIKE '%abc123% " but I need "CONTAINS(Body, 'abc123')"

    return contains;
}

var parameter = Expression.Parameter(typeof(Message), "message");
var expressionFilter = myclass.FilterString("Body","abc123", parameter);
var lambda = Expression.Lambda(expressionFilter, parameter);

//Apply lambda to EF query object
query = query.Where((Expression<Func<Message, bool>>)lambda);

This creates a TSQL LIKE statement, but I would like it to execute the CONTAINS statement.

Anyway, can I change the above method FilterStringto do this?

+4
source share
2 answers

It doesn't seem like you can use vanilla EF, at least according to the source here: http://entityframework.codeplex.com/SourceControl/latest#src/EntityFramework/Core/Common/EntitySql/AST/BuiltInKind.cs

You will need to add Contains to the AST and generate it. Here is a basic tutorial on DBProviders.

+1

, , , , LINQ :

MyDbContext.Database
.SqlQuery<MyTable>("select * from [MyTable] where contains(Body, @p0), "abc123")
0

Source: https://habr.com/ru/post/1542676/


All Articles