使用 LINQ to SQL 忽略 SQL Server 中的重音

2022-01-07 00:00:00 sql-server linq linq-to-sql

如何在使用 LINQ to SQL 对 SQL Server 数据库进行的查询中忽略重音符号(如 ´、`、~)?

How can I ignore accents (like ´, `, ~) in queries made to a SQL Server database using LINQ to SQL?

更新:

仍然没有想出如何在 LINQ 中执行此操作(或者即使可能),但我设法更改了数据库以解决此问题.只需要更改我想要搜索的字段的排序规则.我的整理是:

Still haven't figured out how to do it in LINQ (or even if it's possible) but I managed to change the database to solve this issue. Just had to change the collation on the fields I wanted to search on. The collation I had was:

SQL_Latin1_General_CP1_CI_AS

CI 代表不区分大小写",AS 代表重音敏感".只需要将 AS 更改为 AI 即可使其不区分口音".SQL语句是这样的:

The CI stans for "Case Insensitive" and AS for "Accent Sensitive". Just had to change the AS to AI to make it "Accent Insensitive". The SQL statement is this:

ALTER TABLE table_name ALTER COLUMN column_name column_type COLLATE collation_type

推荐答案

在 SQL 查询中(我记得是 Sql Server 2000+,我记得),您可以通过执行诸如 select MyString, MyId from MyTable where MyString collat​​e Latin1_General_CI_AI =' 来完成此操作啊啊'.

In SQL queries (Sql Server 2000+, as I recall), you do this by doing something like select MyString, MyId from MyTable where MyString collate Latin1_General_CI_AI ='aaaa'.

我不确定这在 Linq 中是否可行,但更熟悉 Linq 的人可能可以翻译.

I'm not sure if this is possible in Linq, but someone more cozy with Linq can probably translate.

如果您对排序和 select/where 查询总是忽略重音没问题,您可以更改表格以在您关注的字段上指定相同的排序规则.

If you are ok with sorting and select/where queries ALWAYS ignoring accents, you can alter the table to specify the same collation on the field(s) with which you are concerned.

相关文章