LINQ如果忽略重读与案例 [英] LINQ Where Ignore Accentuation and Case

查看:117
本文介绍了LINQ如果忽略重读与案例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是通过其中,方法来筛选使用LINQ元素忽略加重和情况下,最简单的方法是什么?

What is the easiest way to filter elements with LINQ through the Where method ignoring accentuation and case?

到目前为止,我已经能够通过调用属性的方法,我不认为是一个好主意,因为它调用了同样的方法为每一个元素(是吗?)忽略套管。

So far, I've been able to ignore Casing by calling methods on the properties, which I dont think is a good idea because it calls the same method for every element (right?).

因此​​,这里就是我有这么远:

So here's what I got so far:

var result = from p in People
             where p.Name.ToUpper().Contains(filter.ToUpper())
             select p;

请告诉我,如果这是一个很好的做法,并忽略加重的最简单的方法。

Please tell me if this is a good practice, and the easiest way to ignore accentuation.

推荐答案

要忽略大小写和重音(变音符号),你可以先定义一个扩展方法是这样的:

To ignore case and accents (diacritics) you can first define an extension method like this:

    public static string RemoveDiacritics(this String s)
    {
        String normalizedString = s.Normalize(NormalizationForm.FormD);
        StringBuilder stringBuilder = new StringBuilder();

        for (int i = 0; i < normalizedString.Length; i++)
        {
            Char c = normalizedString[i];
            if (CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark)
                stringBuilder.Append(c);
        }

        return stringBuilder.ToString();
    }

(从<改良href="http://stackoverflow.com/questions/359827/ignoring-accented-letters-in-string-comparison">Ignoring在字符串比较音字母)

现在你可以运行你的查询:

Now you can run your query:

string queryText = filter.ToUpper().RemoveDiacritics();

var result = from p in People
         where p.Name.ToUpper().RemoveDiacritics() == queryText
         select p;

这是好的,如果你只是遍历在C#中的集合,但如果你正在使用LINQ to SQL是preferable避免非标方法(包括扩展方法)在LINQ查询。这是因为你的code不能转化为有效的SQL,因此SQL Server和它的所有可爱的性能优化上运行。

This is fine if you are just iterating over a collection in C#, but if you are using LINQ to SQL it is preferable to avoid non-standard methods (including extension methods) in your LINQ query. This is because your code cannot be converted into valid SQL and hence run on SQL Server with all its lovely performance optimization.

因为似乎没有被忽视之内口音LINQ到SQL的标准方法,在这种情况下,我建议改变你要搜索是区分和重音不敏感字段类型(CI_AI)。

Since there doesn't seem to be a standard way of ignoring accents within LINQ to SQL, in this case I would suggest changing the field type that you want to search to be case- and accent-insensitive (CI_AI).

使用您的例子:

ALTER TABLE People ALTER COLUMN Name [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AI

您的查询现在应该忽略重音和大小写。

Your query should now ignore accentuation and case.

请注意,你需要暂时卸下领域的任何唯一约束之前的运行上面的查询,如:

Note that you will need to temporarily remove any unique constraints on the field before running the above query, e.g.

ALTER TABLE People DROP CONSTRAINT UQ_People_Name

现在您的LINQ查询,简直是:

Now your LINQ query would simply be:

var result = from p in People
         where p.Name == filter
         select p;

请参阅相关的问题<一href="http://stackoverflow.com/questions/322441/ignoring-accents-in-sql-server-using-linq-to-sql">here.

这篇关于LINQ如果忽略重读与案例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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