views:

107

answers:

3

How to equal two strings case sensitively in Linq to SQL (in a where query)?

Thanks.

+2  A: 

You'll have to make the field in question case-sensitive in SQL Server (or whatever DBMS you use). If you use SQL Server, look for the Collation field property, in there you can set case sensitivity.

Meta-Knight
+1  A: 

This Blog post shows how to do that.

Jens
+2  A: 

You cannot do it solely within LINQ to SQL. From the documentation:

Unsupported System.String Methods in General

Queries do not account for SQL Server collations that might be in effect on the server, and therefore will provide culture-sensitive, case-insensitive comparisons by default. This behavior differs from the default, case-sensitive semantics of the .NET Framework.

The way to do it is in your own query where you specify the collation:

Select...
From Table
Where Column = "Value" COLLATE SQL_Latin1_General_CP1_CS_AS

Note that the collation I'm providing specifies a case-sensitive match (CS).

Thomas