Other than Entity Framework, linq-to-sql can sometimes switch to linq-to-objects under the hood when it encounters method calls that can't be translated to SQL. So if you do
....
.Select(c => new {
Tick = c.Ticker.TrimEnd().TrimStart(),
Address = c.Address.TrimEnd().TrimStart()
you will notice that the generated SQL no longer contains LTRIM(RTRIM())
, but only the field name and that the trims are executed in client memory. Apparently, somehow the LTRIM(RTRIM())
causes a less efficient query plan (surprisingly).
Maybe only TrimEnd()
suffices if there are no leading spaces.
Further, I fully agree with p.s.w.g. that you should go out of your way to try and clean up the database in stead of fixing bad data in queries. If you can't do this job, find the right persons and twist their arms.