I was reading the announcement of entity framework core 2.0 https://blogs.msdn.microsoft.com/dotnet/2017/08/14/announcing-entity-framework-core-2-0/
It says that they added new Sql functions like EF.Functions.Like
for performing the SQL LIKE
operation.
I was wondering, what then would be the difference between EF.Functions.Like
and string.Contains
/StartsWith
?
For example:
var customers = context.Customers.Where(c => c.Name.StartsWith("a")); // Version A
var customers = context.Customers.Where(c => EF.Functions.Like(c.Name, "a%")); // Version B
What would be the difference between the two versions?
EF already knows how to translate string.Contains
/StartsWith
to the corresponding SQL operations, doesn't it?
The only reason i can think of is that EF.Functions.Like would allow for more complex patterns like "a%b%"
(although this one can be written as StartsWith("a") && Contains("b")
)
Is this the reason?
.StartsWith
gets translated toLIKE 'a%'
. Contains on the other hand will be converted toLIKE '%a%'
, which is a very bad idea.LIKE 'a%'
can take advantage of indexes, since it's essentially a range search.LIKE '%a%'
though has to process all rows to find if the pattern matches.Like('abc%def%ghi')
then trying to compose the same with tree different methods.