0

I am trying to know if a property of an entity contains any of the string in a list of strings.

I am trying to do this:

List<string> miLstStrings;   // it is populated with some strings.

List<Data> miLstResultDb = await miDbContext.Data
    .Where(x => miLstStrings.Any(y => x.StringProperty.Contains(y)))
    .ToListAsync()
    .ConfigureAwait(false);

But I get an error telling me that the query can't be translated.

Thanks.

Svyatoslav Danyliv
  • 21,911
  • 3
  • 16
  • 32
Álvaro García
  • 18,114
  • 30
  • 102
  • 193
  • 1
    EF Core has limited support for local collections and you have to build Expression Tree dynamically to make desired filter. You can use this extension [FilterByItems](https://stackoverflow.com/a/67666993/10646316) and replace `Where` with `.FilterByItems(miLstStrings, (x, s) => x.StringProperty.Contains(s), true)` – Svyatoslav Danyliv Jun 22 '22 at 09:17
  • And does this allow to evalute the query in the server instead of the client? – Álvaro García Jun 22 '22 at 09:27
  • 1
    Actually it is main goal why this function is born - generate Expression Tree which will be executed on the server side. – Svyatoslav Danyliv Jun 22 '22 at 09:28

0 Answers0