不区分大小写的 LINQ(没有 toUpper 或 toLower)

发布于 2024-10-22 10:06:45 字数 522 浏览 4 评论 0原文

public Articles GetByName(string name, Categories category, Companies company)
{
    var query = from article in session.Linq<Articles>()
                where article.Name == name &&
                      article.Category == category &&
                      article.Company == company
                select article;
    return query.FirstOrDefault();
}

如何使LINQ查询不区分大小写?我可以使用 toLower()toUpper(),但我想改用 OrdinalIgnoreCase。是否可以?

public Articles GetByName(string name, Categories category, Companies company)
{
    var query = from article in session.Linq<Articles>()
                where article.Name == name &&
                      article.Category == category &&
                      article.Company == company
                select article;
    return query.FirstOrDefault();
}

How to make the LINQ query case-insensitive? I can use toLower() or toUpper(), but I want to use OrdinalIgnoreCase instead. Is it possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

抽个烟儿 2024-10-29 10:06:45

String.Equals 与适当的参数一起使用以使其不区分大小写

mySource.Where(s => String.Equals(s, "Foo", StringComparison.CurrentCultureIgnoreCase));

Use String.Equals with the appropriate parameters to make it case insensitive

mySource.Where(s => String.Equals(s, "Foo", StringComparison.CurrentCultureIgnoreCase));
等风来 2024-10-29 10:06:45

使用 .Equals(name, StringComparison.OrdinalIgnoreCase) 方法代替 ==

var query = from article in session.Linq<Articles>()
            where article.Name.Equals(name, StringComparison.OrdinalIgnoreCase) &&
                  article.Category.Equals(category) &&
                  article.Company.Equals(company)
            select article;

return query.FirstOrDefault();

Instead of == use the .Equals(name, StringComparison.OrdinalIgnoreCase) method.

var query = from article in session.Linq<Articles>()
            where article.Name.Equals(name, StringComparison.OrdinalIgnoreCase) &&
                  article.Category.Equals(category) &&
                  article.Company.Equals(company)
            select article;

return query.FirstOrDefault();
狼性发作 2024-10-29 10:06:45

如果这是针对具有不区分大小写排序规则的数据库的 LINQ to SQL 查询,则它已经是不区分大小写的。请记住,LINQ to SQL 实际上并不执行您的 == 调用;而是执行您的 == 调用。它将其视为一个表达式并将其转换为 SQL 中的等式运算符。

如果它是 LINQ to Objects,那么您可以使用 String.Equals,正如其他发帖者指出的那样。

If this is a LINQ to SQL query against a database with a case-insensitive collation, then it already is case-insensitive. Remember that LINQ to SQL isn't actually executing your == call; it's looking at it as an expression and converting it to an equality operator in SQL.

If it's LINQ to Objects, then you can use String.Equals as the other posters have pointed out.

泪痕残 2024-10-29 10:06:45
var query = from article in session.Linq<Articles>()
           where string.Equals(article.Name,name, StringComparison.OrdinalIgnoreCase) &&
                 string.Equals(article.Category,category, StringComparison.OrdinalIgnoreCase) &&
                 string.Equals(article.Company,company, StringComparison.OrdinalIgnoreCase)
                        select article;

            return query.FirstOrDefault();

它还会处理名称、类别、公司为 null 的情况

var query = from article in session.Linq<Articles>()
           where string.Equals(article.Name,name, StringComparison.OrdinalIgnoreCase) &&
                 string.Equals(article.Category,category, StringComparison.OrdinalIgnoreCase) &&
                 string.Equals(article.Company,company, StringComparison.OrdinalIgnoreCase)
                        select article;

            return query.FirstOrDefault();

It will also handle when Name,Category,Company is null

小巷里的女流氓 2024-10-29 10:06:45

如果您使用的是 C# 6.0,则可以定义在构造 LINQ 语句时使用的简短扩展方法:

public static bool EqualsInsensitive(this string str, string value) => string.Equals(str, value, StringComparison.CurrentCultureIgnoreCase);

用法:

query.Where(item => item.StringProperty.EqualsInsensitive(someStringValue));

对于低于 6.0 的 C#,它将如下所示:

public static bool EqualsInsensitive(this string str, string value) 
{
    return string.Equals(str, value, StringComparison.CurrentCultureIgnoreCase);
}

If you are using C# 6.0 you can define a short extension method to be used when constructing LINQ statements:

public static bool EqualsInsensitive(this string str, string value) => string.Equals(str, value, StringComparison.CurrentCultureIgnoreCase);

Usage:

query.Where(item => item.StringProperty.EqualsInsensitive(someStringValue));

For C# less than 6.0 it will look like this:

public static bool EqualsInsensitive(this string str, string value) 
{
    return string.Equals(str, value, StringComparison.CurrentCultureIgnoreCase);
}
情绪操控生活 2024-10-29 10:06:45

使用

String.Equals(article.Name, name, StringComparison.OrdinalIgnoreCase)

Use

String.Equals(article.Name, name, StringComparison.OrdinalIgnoreCase)
安穩 2024-10-29 10:06:45

将其更改为

public Articles GetByName(string name, Categories category, Companies company)
        {
    var query = from article in session.Linq<Articles>()
                            where string.Equals(article.Name, StringComparison.CurrentCultureIgnoreCase)  == name &&
                                string.Equals(article.Category, StringComparison.CurrentCultureIgnoreCase == category &&
                                string.Equals(article.Company, StringComparison.CurrentCultureIgnoreCase == company
                            select article;

                return query.FirstOrDefault();
}

Change it to

public Articles GetByName(string name, Categories category, Companies company)
        {
    var query = from article in session.Linq<Articles>()
                            where string.Equals(article.Name, StringComparison.CurrentCultureIgnoreCase)  == name &&
                                string.Equals(article.Category, StringComparison.CurrentCultureIgnoreCase == category &&
                                string.Equals(article.Company, StringComparison.CurrentCultureIgnoreCase == company
                            select article;

                return query.FirstOrDefault();
}
在巴黎塔顶看东京樱花 2024-10-29 10:06:45

使用

string.Equals(name, article.Name, StringComparison.OrdinalIgnoreCase) 

您确定您的数据库支持它时

。例如,具有 NOCASE 排序规则的 SQLite 将忽略该选项。
Oracle 使用将要采用的会话设置NLS_COMPNLS_SORT

如果没有文化问题,则可以使用 ToLower()ToUpper()

Use

string.Equals(name, article.Name, StringComparison.OrdinalIgnoreCase) 

when you are sure that your database supports it.

E.g. SQLite with a collate of NOCASE will ignore the option.
Oracle uses session settings NLS_COMP, NLS_SORT that will be taken.

Else use ToLower() or ToUpper() when you have no culture issues.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文