实体框架 - 不区分大小写包含? [英] Entity Framework - case insensitive Contains?

查看:111
本文介绍了实体框架 - 不区分大小写包含?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

LINQ包含不区分大小写

我想要解决这个问题不涉及ToUpper或ToLower,因为我在下面的代码中使用;

I want a solution to this problem that does not involve ToUpper or ToLower, as I use in the code below;

var upper = term.ToUpper();
using (var db = this.DataContext)
{
    return db.Counties.Where(x => x.CountyName.ToUpper().Contains(upper)).ToList();
}

我正在使用有权框架,所以C#解决方案使用 StringComparison.CurrentCultureIgnoreCase 不起作用。它的工作原理是等于 EndsWith StartsWith ,但是不是包含

I am using entitly framework so the C# solution of using StringComparison.CurrentCultureIgnoreCase does not work. It does work for Equals, EndsWith and StartsWith, but not Contains.

推荐答案

我知道这与直接EF,但只有解决方案我可以想到的是将DB表列排序规则更改为不区分大小写,例如:

I know that this is not related directly to EF, but only solution I can think of is to alter DB table column collation to be Case Insensitive e.g.:

ALTER TABLE TABLENAME ALTER COLUMN COLUMNNAME nvarchar(100) COLLATE Latin1_General_CI_AS NULL

CI - 不区分大小写/
CS - 案例敏感

CI - case insensitive / CS - case sensitive

AS - 口音敏感/
AI - 口音敏感(也可用)

AS - accent sensitive / AI - accent sensitive (can also be useful)

如果您无法更改表列的排序规则,则可以使用EF中的直接查询强制整理

If you can't change collation of table column you can use direct query from EF with forcing collation

select * 
from table
where country collate Latin1_General_CI_AS != @country

这篇关于实体框架 - 不区分大小写包含?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆