I have a textbox that allows a user to specify a search string, including wild cards, for example:
Joh*
*Johnson
*mit*
*ack*on
Before using LINQ to Entities, I had a stored procedure which took that string as parameter and did:
SELECT * FROM Table WHERE Name LIKE @searchTerm
And then I would just do a String.Replace('*', '%') before passing it in.
Now with LINQ to Entities I am trying to accomplish the same thing. I know there is StartsWith, EndsWith and Contains support, but it won't support it in the way that I need.
I read about "SqlMethods.Like" and tried this:
var people = from t in entities.People
where SqlMethods.Like(t.Name, searchTerm)
select new { t.Name };
However I am getting the following exception:
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.
How would I get this same functionality using LINQ to Entities?
var people = entities.People.Where("it.Name LIKE @searchTerm", new ObjectParameter("searchTerm", searchTerm));