Linq will automatically crop my string!

I have this basic linq query where I want to get the city from the database. The problem is that my search string is truncated without my query. I have simplified this as much as possible. Example:

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

City.City_Code is "LAS", not "LAS", but it gets the city with City_Code "LAS".

How can i solve this? I also tried Equals, but the result is the same.

+4
source share
1 answer

This is not a problem with LINQ. This is how the database compares strings.

If the lines do not have the same length, the shorter line is filled with spaces when comparing them, so the lines "LAS" and "LAS " are considered equal.

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

You can get around this by adding another character to the lines:

 where city.City_Code + "." == "LAS ." 
+7
source

All Articles