0

Please help with the following result. I am using linq to perform a query. I know the String.Equals(string) is case sensitive, and I have tested it. However, when I am applying String.Equals functions in the linq statement below, the result is case insensitive. And I can not find what went wrong.

In the result below, I am searching for "qiao", however, "Qiao" was returned as a valid result. And it is not expected. Please help.

Code and debugging results

Qiao Li
  • 189
  • 1
  • 1
  • 10
  • Does this answer your question? [https://stackoverflow.com/questions/43277868/entity-framework-core-contains-is-case-sensitive-or-case-insensitive) – Neil Apr 22 '21 at 16:25
  • If it is the database and i am doing code first, what i can do from the c# side to make the db case sensitive? – Qiao Li Apr 22 '21 at 16:36
  • Here's the official documentation https://learn.microsoft.com/en-us/ef/core/miscellaneous/collations-and-case-sensitivity – Neil Apr 22 '21 at 16:43
  • Does the Contact class in c# have a custom Equal method? – jdweng Apr 22 '21 at 16:43
  • You can do the case insensitive check on the DB then do a case sensitive check on the client side like `query.AsEnumerable().Where(x => x.Name == "qiao")` – juharr Apr 22 '21 at 16:45

2 Answers2

0

for case-sensitive result you can try == operator instead of .Equals() method -

var query = from c in db.Contacts where c.Name == "qiao" select c;
-1

You can use ToUpperCase() or ToLowerCase to solve case sensitivity problem. Example-

var query = from c in db.Contacts where c.Name.ToUpperCase().Equals("QIAO")
select c;
ssilas777
  • 9,672
  • 4
  • 45
  • 68