Adding all expressions inside the loop as OR condition to the IQueryable object C#

Viewed 26

Adding all expressions inside the loop as OR condition to the IQueryable object

I have a database table and I want to check whether there is a name within the keywords given by the user from the names in the table.

For example; Database table name: Users Column names: Id, Name The data inside is: (Id:1, Name:Tom), (Id:2, Name: John), (Id:3, Name: Harry)

And the keywords from the user: "to" and "harr" Values ​​that should return: Tom and Harry

The code I tried:

var queryableUsers = db.Users.AsQueryable();
queryableUsers = queryableUsers .Where(x => values.Any(v => x.Name.ToLower().Contains(v)));

But here I am getting error:

.Name.ToLower().Contains(v)' 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'.

Here is what I want to do:

Expression<Func<User, bool>> predicate = (User) => false;
foreach(var v in values)
    predicate = predicate.Or(x => x.Name.Contains(term));

But Or is not working. How do I make this possible in c#?

0 Answers
Related