LINQ的自动裁剪我的字符串! [英] Linq automatically trims my string!

查看:144
本文介绍了LINQ的自动裁剪我的字符串!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个基本的LINQ查询,我想从数据库中城市。问题是,我的搜索​​字符串没有我要求它修剪。我已经简化它尽可能多地。例如:

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;

该city.City_ code为LAS,而不是LAS,它仍然得到城市的City_ codeLAS。

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.

推荐答案

这是不是与LINQ的问题。这是怎样的数据库比较字符串。

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

如果字符串不具有相同的长度,所述较短的字符串与空格填充时他们相比,这样的弦LASLAS被认为是相等的。

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.

请参阅: http://support.microsoft.com/kb/316626

您可以解决此通过增加另一个字符字符串:

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

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

这篇关于LINQ的自动裁剪我的字符串!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆