Home >Backend Development >C++ >How to Perform Case-Sensitive Comparisons with LINQ to Entities?

How to Perform Case-Sensitive Comparisons with LINQ to Entities?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2025-01-21 17:56:10793browse

How to Perform Case-Sensitive Comparisons with LINQ to Entities?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn