How to perform a case-sensitive LINQ query in Azure?

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-11 12:32:49

问题


I am using Windows Azure Storage Tables, and want to query for an object. The user inputs a string, which I look for in the database as such:

var myKey = "SomeCaseSensitiveKeyInputByTheUser";

var someObject = (from o in dataContext.Objects
    where o.SomeString.Equals(myKey)
    select o).FirstOrDefault();

However, for some reason, all string comparisons appear to be case insensitive (both == and string.Equals()). However, I need to match the exact casing of the user input string.

How can I do this in my LINQ query?


回答1:


Using == is the same as .Equals(..), as it just calls that method. You can force to use a case sensitive comparison using an overload of Equal() passing a string.comparison enum

CurrentCulture                   Compare strings using culture-sensitive sort rules and the current culture.
CurrentCultureIgnoreCase         Compare strings using culture-sensitive sort rules, the current culture, and ignoring the case of the strings being compared.
InvariantCulture                 Compare strings using culture-sensitive sort rules and the invariant culture.
InvariantCultureIgnoreCase       Compare strings using culture-sensitive sort rules, the invariant culture, and ignoring the case of the strings being compared.
Ordinal                          Compare strings using ordinal sort rules.
OrdinalIgnoreCase                Compare strings using ordinal sort rules and ignoring the case of the strings being compared.

more info at:

http://msdn.microsoft.com/en-us/library/system.stringcomparison.aspx



来源:https://stackoverflow.com/questions/10703142/how-to-perform-a-case-sensitive-linq-query-in-azure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!