4

I have this basic linq query where I want to get a city from the database. The problem is that my search string is trimmed without me asking for it. I've simplified it as much as possible. Example:

var firstCity = 
    from city in db.Cities
    where city.City_Code == "LAS "
    select city;

The city.City_Code is "LAS" and not "LAS ", still it gets the city with the City_Code "LAS".

How do I solve this? I've also tried Equals, but the result is the same.

Steven
  • 166,672
  • 24
  • 332
  • 435
Gerhard
  • 41
  • 1

1 Answers1

7

This is not an issue with LINQ. It's how the database compares strings.

If the strings doesn't have the same length, the shorter string is padded with spaces when they are compared, so the strings "LAS" and "LAS " are considered to be equal.

See: http://support.microsoft.com/kb/316626

You could work around this by adding another character to the strings:

where city.City_Code + "." == "LAS ."
Guffa
  • 687,336
  • 108
  • 737
  • 1,005