How can I perform a LIKE search with entity framework 6 and npgsql?
I tried
1. SQLMethods.Like
queryable.Where(entity => SqlMethods.Like(entity.Name, "%EW%6%"));
but I got the message
LINQ to Entities does not recognize the method 'Boolean Like(System.String, System.String)' method, and this method cannot be translated into a store expression.
2. linq extension method
Then I tried the linq extension method WhereLike
from this stackoverflow post: https://stackoverflow.com/a/27153779/1489968
It's working, but not for all cases, because it's cutting the search string into several pieces.
queryable.WhereLike(t => t.Name, "%EW%6%"));
is transformed into two and-connected Contains
calls, that are transformed into two LIKE
expressions.
SELECT ...
WHERE ... AND
("Extent1"."name" LIKE E'%EU%' AND "Extent1"."name" LIKE E'%6%')
which means "All entities with the name containing 'EU' and '6'"
But I want the transformation to result in a query like the following one:
SELECT ...
WHERE ... AND
("Extent1"."name" LIKE E'%EW%6%')
which means "All entities with the name containing 'EU' followed by a string, that contains '6'"