Linq 自动修剪我的字符串!
我有一个基本的 linq 查询,我想从数据库中获取一个城市。问题是我的搜索字符串在我没有要求的情况下就被修剪了。我已经尽可能地简化了它。示例:
var firstCity =
from city in db.Cities
where city.City_Code == "LAS "
select city;
city.City_Code 是“LAS”而不是“LAS”,但它仍然会获取 City_Code 为“LAS”的城市。
我该如何解决这个问题?我也尝试过等于,但结果是一样的。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是 LINQ 的问题。这就是数据库比较字符串的方式。
如果字符串长度不同,则比较时较短的字符串会用空格填充,因此字符串
"LAS"
和"LAS "
被视为等于。请参阅:http://support.microsoft.com/kb/316626
您可以通过以下方式解决此问题添加另一个字符到字符串:
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: