I already had a look at the following question, but it didn't gian much traction and the problem remains unsolved. How to combine (OR) two expression trees
How do you or
-concatenate two Expression<Func<T, bool>>
.
let's consider the simplest case:
var fulltext = "some text";
private Expression<Func<User, bool>> FulltextSearch(string fulltext) {
Expression<Func<User, bool>> left = t => t.Name.Contains(fulltext)
Expression<Func<User, bool>> right = t => t.Description.Contains(fulltext)
return Expression.Lambda<Func<Task, bool>>(Expression.Or(left.Body, right.Body), left.Parameters);
}
var users = dbContext.Users
.Where(FulltextSearch("name xy"))
.ToList();
this is a simplified example!
This ends in an error
System.InvalidOperationException: The LINQ expression 'DbSet<User>()
.Where(t => t.Name.Contains(__fulltext_0) | t.Description.Contains(__fulltext_0))' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'...
But you can easily write this query like this and it works:
var users = dbContext.Users
.Where(u => u.Name.Contains(fulltext) || u.Description.Contains(fulltext))
.ToList()
So how do you combine two Expression<Func<T,bool>> into one such that ef core can translate it into SQL?