Roy de Jong
Roy de Jong

Reputation: 317

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

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?

Upvotes: 1

Views: 1484

Answers (1)

Massimiliano Peluso
Massimiliano Peluso

Reputation: 26727

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

Upvotes: 1

Related Questions