Manikandan
Manikandan

Reputation: 627

EF.Functions.Like() for an array of string

I want to filter the IQueryable<T> with the help of EF.Functions.Like() method, which accepts a string parameter, to make use of array of strings. Also, I want this filter to be applied on an IQueryable and not on a List.

var configurations = _dbContext.Configurations
    .Include(x => x.ChildTable)
    .Where(x => x.Id == Id);

if (!string.IsNullOrEmpty(request.Filter))
{
    configurations = configurations.Where(x => EF.Functions.Like(string.Join(", ", x.ChildTable.Select(x => x.Name).ToArray()), '%' + request.Filter + '%'));
}

In the above code, Configuration has a one-many relationship with the ChildTable. Meaning, each configuration will have a more than one ChildTable entries related. So, with that given, x.ChildTable.Select(x => x.Name) is of type IEnumerable<string>. And, I want to filter the Configuration records whose ChildTable.Name entries is like the given request.Filter.

Upvotes: 0

Views: 1901

Answers (1)

Svyatoslav Danyliv
Svyatoslav Danyliv

Reputation: 27416

Try the following query:

var configurations = _dbContext.Configurations
    .Include(x => x.ChildTable)
    .Where(x => x.Id == Id);

if (!string.IsNullOrEmpty(request.Filter))
{
    configurations = configurations
        .Where(x => x.ChildTable.Any(c => EF.Functions.Like(c.Name, '%' + request.Filter + '%')));
}

Upvotes: 2

Related Questions