0

I want the users of our web app to search for multiple keywords in one search textbox seperated by a space, example: "jean rebecca simpson".

In the backend we have EF Core and a DbContext having a DbSet and a queryhandler that builds an IQueryable.

The function is like this, where the keywords are passed in an enumerable list and the columns can be defined using a private enumerable so that it can be easly changed with more or less columns in the feature.

private static IQueryable<Student> ApplyFilter(IQueryable<Student> query, IEnumerable<string> keywords)
        {
            foreach (var keyword in keywords)
            {
                foreach (var searchColumn in GetSearchColumns())
                {
                    query = query.Where(v => EF.Functions.Like(searchColumn, $"%{keyword}%"));
                }
            }

            return query;
        }
    
private static IEnumerable<string> GetSearchColumns()
        {
            yield return nameof(Student.SurName);
            yield return nameof(Student.Gender);
            yield return nameof(Student.LastName);
        }

I want the IQueryable to use conditional ORs, in the way it was like this:

query = query.Where(v => EF.Functions.Like("LastName", $"%{keywords[0]%" || EF.Functions.Like("LastName", $"%{keywords[1]%")

How can this be accomplished ? The looped Where clause is acting like a conditional AND construct.

Patrick Peters
  • 9,456
  • 7
  • 57
  • 106

0 Answers0