The issue is that you are trying to do a string.Contains
within an Any
expression which EF will choke on trying to compose down to SQL. Cleptus is on the nose, to build a predicate for the Where
clause OR-ing the term comparisons. Otherwise your code should work without the contains check, but rather an equality check:
Without Contains
: (equality check rather than LIKE %name%
)
var terms = title.Split(' ').ToList();
items = items.Where(w => terms.Contains(w.ItemName)); // IN clause.
Built expression:
var terms = title.Split(' ').ToList();
Expression<Func<Item, bool>> predicate = (Item) => false;
foreach(var term in terms)
predicate = predicate.Or(x => x.ItemName.Contains(term));
items = items.Where(predicate);
So for each term in the title, we OR a match using a LIKE %term% on the ItemName.