1

I`ve got a query and in one part i need to add this:

List<string> list = *some list of strings*
query = query.Where(a => list.Any(x => EF.Function.ILike(a.Name, $"{x}%")));

It worked before, but after update my ef library i got this error:

The LINQ expression 'DbSet ...' 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'.

How can I change my query for workable state again? I noticed that without .Any() it works.

query = query.Where(a => EF.Function.ILike(a.Name, "string1%") || EF.Function.ILike(a.Name, "string2%"));

But I cant do it manually. Is there a proper way to fix it?

Mateech
  • 1,010
  • 1
  • 11
  • 26

1 Answers1

1

Use this my answer for extension method FilterByItems. Then you can do the following:

List<string> list = *some list of strings*
var patterns = list.Select(x => x + "%");

var result = query
    .FilterByItems(patterns, (a, p) => EF.Function.ILike(a.Name, p), true);
Svyatoslav Danyliv
  • 21,911
  • 3
  • 16
  • 32