如何使用.Contains的实体框架搜索字符串并且不区分重音 [英] How to search string using Entity Framework with .Contains and with accent-insensitive

查看:222
本文介绍了如何使用.Contains的实体框架搜索字符串并且不区分重音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的数据库中,我有一个可以存储城市的表格。一些城市有像Foz doIguaçu这样的口音。

In my database, I have a table that stores cities. Some cities have accents like "Foz do Iguaçu".

在我的MVC应用程序中,我有一个JSON返回一个单词的城市列表,但是很少有用户没有使用重音搜索城市,例如Foz do Iguacu。

In my MVC application, I have a JSON that return a list of cities based in a word, however, few users aren't using accents to search for the city, for example "Foz do Iguacu".

在我的数据库中我有Foz do Igua Ç u,但用户
用户搜索Foz do Igua C u

in my database I have "Foz do IguaÇu" but users users searches for "Foz do IguaCu"

如何在表格中搜索记录,忽略口音?

How can I search records in my table, ignoring accents?

这是我的代码:

    using (ServiciliEntities db = new ServiciliEntities())
    {
        List<Cidades> lCidades = db.Cidades.Where(c => c.CidNome.ToLower().Contains(q.Trim().ToLower())).OrderBy(c => c.CidNome).Take(10).ToList();
        ArrayList lNomes = new ArrayList();
        foreach (Cidades city in lCidades)
            lNomes.Add(new {city.CidNome, city.Estados.EstNome});

        return Json(new { data = lNomes.ToArray() });
    }


推荐答案

解决方案如下:

ALTER TABLE dbo.YourTableName
ALTER COLUMN YourColumnName NVARCHAR (100) COLLATE SQL_Latin1_General_CP1_CI_AI NULL

其中LATIN1_GENERAL表示英语(美国),CI表示不区分大小写,AI表示重音不敏感。

Where LATIN1_GENERAL means English (US), CI means Case-Insensitive and AI means Accent-Insensitive.

这篇关于如何使用.Contains的实体框架搜索字符串并且不区分重音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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