Home > Back-end >  How to build an Expression for FullText Search with EF Core?
How to build an Expression for FullText Search with EF Core?

Time:12-15

Given a PropertyName and a Value to search for how can I build an Expression to work with?

Outcome has to be EF.Functions.Contains(PropertyName, Value).

In the end I am applying the expression to a IQueryable.

Thank you!

CodePudding user response:

Contains method for FullText search located in SqlServerDbFunctionsExtensions

var PropertyName = "SomeProp";
var Value = "SomeValue";

var parameter = Expression.Parameter(typeof(SomeType), "c");
var propertyPath = PropertyName.Split('.')
    .Aggregate((Expression)parameter, Expression.Property);

var callExpression =  Expression.Call(
    typeof(SqlServerDbFunctionsExtensions),
    nameof(SqlServerDbFunctionsExtensions.Contains),
    Type.EmptyTypes,
    Expression.Constant(EF.Functions),
    propertyPath,
    Expression.Constant($"\"*{Value}*\"")
);
  • Related