简体   繁体   English

为什么string.Equals默认情况下不区分大小写?

[英]Why does string.Equals work for case insensitivity by default?

I have tested the following expressions to evaluate and compare (2) strings: 我测试了以下表达式来评估和比较(2)字符串:

.Where(x => x.GetAttributeValue<string>("country").Equals("United States"))
.Where(x => x.GetAttributeValue<string>("country").Equals("UNITED STATES"))
.Where(x => x.GetAttributeValue<string>("country").Equals("uniteD sTates"))

NOTE: These queries are expressions built to retrieve data using the CRM LINQ provider from its SDK. 注意:这些查询是为使用SDK中的CRM LINQ提供程序检索数据而构建的表达式。

The value of x.GetAttributeValue<string>("country") is exactly United States . x.GetAttributeValue<string>("country")值恰好United States All of the expressions above return true . 上面的所有表达式都返回true Honestly this is fine and even great with me, but doesn't seem to hold true against the MSDN documentation on string.Equals which states: 老实说,这对我很好,甚至很好,但似乎不符合string.Equals上的MSDN 文档 ,其中指出:

This method performs an ordinal (case-sensitive and culture-insensitive) comparison. 此方法执行序数(区分大小写和文化不敏感)比较。

I would expect to have to do the following which would ignore overall casing: 希望不得不做以下几点忽略整体外壳:

.Where(x => x.GetAttributeValue<string>("country").Equals("UnITed sTAtes", StringComparison.OrdinalIgnoreCase))

Interestingly, the above always returns false . 有趣的是,上面总是返回false To me this is backwards. 对我来说,这是倒退的。 The default implementation is not taking into account case sensitivity, and the explicit assignment of StringComparison.OrdinalIgnoreCase is acting like it's case sensitive and thus not matching the exact value of United States . 默认实现不考虑区分大小写,并且明确分配StringComparison.OrdinalIgnoreCase行事就像是大小写敏感的,因此不匹配的精确United States

Why is this behavior reverse or am I misunderstanding this? 为什么这种行为反过来或者我误解了这个?

Are you using LINQ-To-Sql? 你在使用LINQ-To-Sql吗? If so, that code is actually building an expression tree that is then translated into SQL statements that are run on Sql Server. 如果是这样,该代码实际上构建了一个表达式树,然后将其转换为在Sql Server上运行的SQL语句。 Sql Server, like most database engines, does not care about case by default. 与大多数数据库引擎一样,Sql Server默认情况下不关心大小写。

If this were Linq-To-Objects, I expect you'd get different results. 如果这是Linq-To-Objects,我希望你得到不同的结果。

Actually, all LINQ expressions against CRM context is converted by LINQ provider to QueryExpression . 实际上,针对CRM上下文的所有LINQ表达式都由LINQ提供程序转换为QueryExpression And string.Equals filter is defined there as case insensitive, so you get this result. 并且string.Equals过滤器在那里被定义为不区分大小写,因此您得到此结果。

About your attempt with StringComparison.OrdinalIgnoreCase , IMO it could be (and I think it is) that the provider does not support that method with a second parameter (I've tried and it does not return desired result with other comparison types either). 关于你使用StringComparison.OrdinalIgnoreCase的尝试,IMO可能(并且我认为是)提供者不支持带有第二个参数的方法(我已经尝试过它并没有返回其他比较类型所需的结果)。

You may try to use 你可以尝试使用

x.GetAttributeValue<string>("country") == "United States"

instead of 代替

x.GetAttributeValue<string>("country").Equals("United States")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM