C# 比较字符串时如何忽略大小写?

C# 比较字符串时如何忽略大小写?,c#,C#,我使用linq搜索列表(用户在文本框中输入查询) 我希望这是不区分大小写,并试图使用IgnoreCase,但我不知道把它放在哪里。。。。我知道我可以使用上限或下限,但我想知道是否有人有其他方法?什么是最佳实践? 正则表达式似乎也不起作用 string searchQuery = tbSearchQuery.Text; var something= from x in y where x.Subject.Contain

我使用linq搜索列表(用户在文本框中输入查询)

我希望这是不区分大小写,并试图使用IgnoreCase,但我不知道把它放在哪里。。。。我知道我可以使用上限或下限,但我想知道是否有人有其他方法?什么是最佳实践? 正则表达式似乎也不起作用

        string searchQuery = tbSearchQuery.Text;
        var something= from x in y 
                       where x.Subject.Contains(searchQuery)
                       select x;

我使用以下自己制作的扩展(用于普通字符串)

HTH

试试看

string searchQuery = tbSearchQuery.Text; 
var something= from x in y  
               where x.Subject.IndexOf(searchQuery, StringComparison.OrdinalIgnoreCase) != -1

您能改用
IndexOf

string searchQuery = tbSearchQuery.Text;
var something= from x in y 
               where x.Subject.IndexOf(searchQuery, StringComparison.OrdinalIgnoreCase) >= 0
               select x;
使用:


这也可以处理字符串为null的情况。

如果您使用的是
LINQ to SQL
实体框架
,则排序规则是固定的,并在表定义中设置。比较它们的唯一方法是
ToUpper
(或
ToLower
,但最好是
ToUpper
,如本文所述)比较的两部分。

我支持扩展方法。我有一门课要用。只要把它放在你的项目中,引用你的名字空间,你就可以走了

using System;

namespace YOUR NAME SPACE
{
    public static class StringExtensionMethods
    {
        /// <summary>
        /// Extention method to allow a string comparison where you can supply the comparison type 
        /// (i.e. ignore case, etc).
        /// </summary>
        /// <param name="value">The compare string.</param>
        /// <param name="comparisionType">The comparison type - enum, use OrdinalIgnoreCase to ignore case.</param>
        /// <returns>Returns true if the string is present within the original string.    </returns>
        public static bool Contains(this string original, string value, StringComparison comparisionType)
        {
            return original.IndexOf(value, comparisionType) >= 0;
        }
    }
}
var something = from x in y 
                where string.Equals(x.Subject, searchQuery, StringComparison.CurrentCultureIgnoreCase)
                select x;
使用系统;
命名您的名称空间
{
公共静态类StringExtensionMethods
{
/// 
///扩展方法,以允许在其中提供比较类型的字符串比较
///(即忽略案例等)。
/// 
///比较字符串。
///比较类型-enum,使用OrdinalIgnoreCase忽略大小写。
///如果原始字符串中存在该字符串,则返回true。
公共静态bool包含(此字符串原始、字符串值、StringComparison ComparisonType)
{
返回original.IndexOf(value,comparisontype)>=0;
}
}
}
下面是一个使用它的LINQ查询示例:


var结果=来自促销的响应
哪里
String.IsNullOrEmpty(find.Code)||
(promotion.Code!=null&&promotion.Code.Contains(find.Code,StringComparison.OrdinalIgnoreCase))
哪里
String.IsNullOrEmpty(find.Name)||
(promotion.Name!=null&&promotion.Name.Contains(find.Name、StringComparison.OrdinalIgnoreCase))
选择促销活动;

既然还没有人把它放上去,我建议使用static,这样你就不必担心
null
,只需要返回你想要的信息

也可以,但您不需要对字符串进行排序(整数返回值的原因),只需在不区分大小写的比较中确定它们的值是否相等

var something = from x in y 
                where string.Equals(x.Subject, searchQuery, StringComparison.CurrentCultureIgnoreCase)
                select x;

这实际上有点取决于这里的
y
是什么。。。i、 这是LINQ到对象吗-SQL-EF?不同的实现将支持不同的东西。好吧,你可以在where子句中添加一个空检查。最初的问题不是关于EF的。根据您的DB设置,字符串比较默认情况下通常区分大小写,因此您可以使用问题中的原始代码,它对于EF应该可以正常工作。对,我只是让其他人知道,因为我看到了一条投票结果较高的注释。我不知道为什么会接受这一点——这不是正确的答案。这段代码返回的所有字符串要么等于查询,要么排序顺序高于查询。当查询为“Adam”时,它返回“Bill”。这只有在您检查
==0
时才有用,这将只返回相等的字符串,但是a)
等于
会更好;和b)问题是关于返回包含查询的字符串,而不仅仅是那些完全匹配的字符串。String.Compare用于确定相对排序顺序的预期目的,而是关于从搜索查询中查找匹配项。我的评论不是虚假的(在底部)。这些结果是不正确的,因为用户没有搜索类似于“Bill”或“Charlie”的内容。它也应该返回“1adam”,但它没有返回。正确,因为它成功通过了测试:。它不工作。正如nmclean所说,如果您使用“==0”,那么它可以工作,但是当您使用“>=0”时,返回的结果太多
using System;

namespace YOUR NAME SPACE
{
    public static class StringExtensionMethods
    {
        /// <summary>
        /// Extention method to allow a string comparison where you can supply the comparison type 
        /// (i.e. ignore case, etc).
        /// </summary>
        /// <param name="value">The compare string.</param>
        /// <param name="comparisionType">The comparison type - enum, use OrdinalIgnoreCase to ignore case.</param>
        /// <returns>Returns true if the string is present within the original string.    </returns>
        public static bool Contains(this string original, string value, StringComparison comparisionType)
        {
            return original.IndexOf(value, comparisionType) >= 0;
        }
    }
}

var results = from promotion in response
              where
                  String.IsNullOrEmpty(find.Code) ||
                  (promotion.Code != null && promotion.Code.Contains(find.Code, StringComparison.OrdinalIgnoreCase))
              where
                  String.IsNullOrEmpty(find.Name) ||
                  (promotion.Name != null && promotion.Name.Contains(find.Name, StringComparison.OrdinalIgnoreCase))
              select promotion;
var something = from x in y 
                where string.Equals(x.Subject, searchQuery, StringComparison.CurrentCultureIgnoreCase)
                select x;