5

Is it possible to set entity framework string comparison case insensitive by default?

If I use

string.StartsWith("stringToCompare", StringComparison.CurrentCultureIgnoreCase)

it works. But when I need to use

string.Contains("strigToCompare")

it doesn't have an overload.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Andre Nascentes
  • 391
  • 4
  • 14
  • 1
    SQL is anyway case insensitive and EF is only delegating its queries it SQL, EF does not control case, I think it's your database's collation problem. – Akash Kava Feb 12 '12 at 05:24

1 Answers1

3

You can simply change the case of both fields to Upper Case:

String stringToCompare = "Some String";

string.ToUpper().Contains(stringToCompare.ToUpper())

This will make the search case-insensitive by converting all case to upper. Of course, ToLower() would work as well.

Nate Stone
  • 521
  • 5
  • 15
  • This would break for the test "Foo" & "Foobar" . given that foobar contains foo. This may not be the equality you are looking for. – Jafin Jan 17 '13 at 03:41
  • 4
    The poster is asking for case-insensitivity when using the Contains() function. The "Foo" & "Foobar" example would return "true" which is the desired result. – Nate Stone Feb 20 '13 at 04:02