3

I can't understand why:

In db I have record in Members table with Name = "Bob"

var a = await repository.Members.FirstOrDefaultAsync(x => x.Name == "BOB"); //is true (Entity Framework Core)

var c = repository.Members.FirstOrDefault(x => x.Name == "BOB"); //is true (LINQ)

var b = "Bob" == "BOB"; //is false
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
A. Gladkiy
  • 3,134
  • 5
  • 38
  • 82
  • 11
    It's not "in LINQ" - it's "in the specific LINQ provider you're using, against the specific database you're using". – Jon Skeet Feb 01 '17 at 07:37
  • 1
    Possible duplicate of [linq to entities case sensitive comparison](http://stackoverflow.com/questions/3843060/linq-to-entities-case-sensitive-comparison) – uTeisT Feb 01 '17 at 07:39
  • 3
    It's a SQL comparison, not LINQ. Sensitivity is controlled by the column's collation. – Panagiotis Kanavos Feb 01 '17 at 07:43

1 Answers1

14

The problem has nothing to do with LINQ. LINQ is just an abstraction mechanism allowing you to query different datasources using the same syntax. If you are using MSSQL by default it will have SQL_Latin1_General_CP1_CI_AS collation which is case insensitive (Notice the CI in the name). So when your LINQ query gets translated to SQL (select * from Members where Name = 'BOB') and run it will return results. So basically you might need to change the collation of your SQL server database to one that is case sensitive if you want to perform case sensitive lookups with it.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 2
    Just as a side note, as per the flagged duplicate question, changing the collation of the column queried against should suffice, instead of changing collation of whole db. – uTeisT Feb 01 '17 at 07:51