How to equal two strings case sensitively in Linq

2019-06-01 20:28发布

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

Thanks.

3条回答
我想做一个坏孩纸
2楼-- · 2019-06-01 20:56

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.

查看更多
Lonely孤独者°
3楼-- · 2019-06-01 20:58

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

Select * from tblemp Where empname='nAveen' COLLATE SQL_Latin1_General_Cp1_CS_AS
查看更多
【Aperson】
4楼-- · 2019-06-01 21:01

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).

查看更多
登录 后发表回答