Besides the Entity Framework, linq-to-sql can sometimes switch to linq-to-objects under the hood when it encounters method calls that cannot be translated into 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 trimmers are executed in client memory. Apparently, somehow LTRIM(RTRIM()) calls a less efficient query plan (surprisingly).
Maybe only TrimEnd() enough if there are no leading spaces.
In addition, I totally agree with pswg that you should go out of your way to try to clear the database instead of committing bad data in queries. If you cannot do this work, find the right people and turn their hands.
source share