The Question
When I write with Linq to entities
context.MyTable.Where(t => t.Name == "Test");
it translates to the following sql:
select * from MyTable where Name = 'Test'
I want to write an expression in Linq to entities that will translate to:
select * from MyTable where Name like 'Test'
Is there some way to achieve that?
Note- I also tried Equals
and CompareTo == 0
but to no avail.
The back story
The reason I want to use Like
instead of =
is because of the fact that white spaces are ignored at the end of the string if you use =
, but it works if you use Like
(see this question: Why the SQL Server ignore the empty space at the end automatically?, and also this: Linq to Entity comparing strings ignores white spaces).
Edit: Why it's not a duplicate of How to do SQL Like % in Linq?
That question asks for like %, which can be done with Contains/StartsWith/EndsWith
, but it's not the same question, I want complete equality so those will not help me.
There was an answer on that question that did look promising though, using SqlMethods.Like
, so I tried
context.MyTable.Where(t => SqlMethods.Like(t.Name, "Test")
but I got the following error:
{"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."}