Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

== by default is case insensitive in c# linq

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
like image 732
A. Gladkiy Avatar asked Nov 30 '25 11:11

A. Gladkiy


1 Answers

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.

like image 141
Darin Dimitrov Avatar answered Dec 02 '25 00:12

Darin Dimitrov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!