Given this linq query in the context of EF data:
var customers = data.Customers.Where(c => c.EmailDomain.StartsWith(term))
You expect it to generate SQL like this, right?
SELECT {cols} FROM Customers WHERE EmailDomain LIKE @term+'%'
Well, actually, he does something like this:
SELECT {cols} FROM Customer WHERE ((CAST(CHARINDEX(@term, EmailDomain) AS int)) = 1)
Do you know why?
Also, replacing the Where selector with:
c => c.EmailDomain.Substring(0, term.Length) == term
it works 10 times faster, but it creates a pretty pretty yqy.
NOTE. Linq to SQL correctly translates StartsWith to Like {term}%, and nHibernate has a special expression for the expression.
sql entity-framework
thatismatt
source share