Detailed explanation of case sensitivity comparison in LINQ to Entities
The following LINQ to Entities query:
<code class="language-c#">Thingies.First(t => t.Name == "ThingamaBob");</code>
Because LINQ to Entities converts lambda expressions into SQL statements, no case-sensitive comparisons are performed. The SQL_Latin1_General_CP1_CI_AS collation used by SQL Server by default is not case-sensitive.
Server-side solution
To enable case-sensitive comparisons, use the following SQL command to change the collation of the Name column in the Thingies table to COLLATE Latin1_General_CS_AS:
<code class="language-sql">ALTER TABLE Thingies ALTER COLUMN Name VARCHAR(25) COLLATE Latin1_General_CS_AS</code>
Client solution
Alternatively, you can use LINQ to Objects to perform a case-sensitive comparison, but this is less efficient:
<code class="language-c#">Thingies.Where(t => t.Name == "ThingamaBob") .AsEnumerable() .First(t => t.Name == "ThingamaBob");</code>
The above is the detailed content of How to Perform Case-Sensitive Comparisons with LINQ to Entities?. For more information, please follow other related articles on the PHP Chinese website!