可以LINQ表达式不区分大小写 [英] can linq expression be case insensitive

查看:288
本文介绍了可以LINQ表达式不区分大小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在利用这个项目使用的jqGrid过滤和排序收藏。该一人失踪的特点是,这个例子是没有做区分大小写的搜索,我所需要的。



因此​​,如果测试用户类型我希望它匹配TEST ,测试等。



我的代码看起来像这样:

 情况下WhereOperation .Equal:
条件= Expression.Equal(memberAccessToString,过滤器);
波长= Expression.Lambda(条件参数);
中断;
情况下WhereOperation.NotEqual:
条件= Expression.NotEqual(memberAccessToString,过滤器);
波长= Expression.Lambda(条件参数);
中断;
情况下WhereOperation.Contains:
条件= Expression.Call(memberAccessToString,
typeof运算(字符串).GetMethod(包含),
Expression.Constant(值));
波长= Expression.Lambda(条件参数);
中断;



反正是有有下面是区分大小写所以测试就等于TEST这些检查

  Expression.NotEqual 
Expression.Equal
Expression.Call(memberAccessToString,
typeof运算(字符串).GetMethod(包含),


解决方案

不幸的是, BCL没有一个包含重载允许您指定情况下不变性。你必须抓住的IndexOf 作为一种变通方法(检查是否结果的IndexOf 大于零):

  VAR的MethodInfo 
= typeof运算(字符串)
.GetMethod(的IndexOf,
新的[] {typeof运算(字符串)的typeof(StringComparison)});

的MethodInfo 接受字符串 StringComparison ,这将允许您指定 StringComparison.OrdinalIgnoreCase 如果你想。


i am leveraging this project to use jqgrid to filter and sort collections. The one missing feature is that this example is not doing case insensitive search which i need.

So if a user types in "Test" i want it to match with "TEST", "TeST", etc . .

i have code that looks like this:

            case WhereOperation.Equal:
                condition = Expression.Equal(memberAccessToString, filter);
                lambda = Expression.Lambda(condition, parameter);
                break;
            case WhereOperation.NotEqual:
                condition = Expression.NotEqual(memberAccessToString, filter);
                lambda = Expression.Lambda(condition, parameter);
                break;
            case WhereOperation.Contains:
                condition = Expression.Call(memberAccessToString,
                    typeof(string).GetMethod("Contains"),
                    Expression.Constant(value));
                lambda = Expression.Lambda(condition, parameter);
                break;

is there anyway to have these checks below being case insensitive so "Test" would equal "TEST"

Expression.NotEqual    
Expression.Equal
Expression.Call(memberAccessToString,
                    typeof(string).GetMethod("Contains"),

解决方案

Unfortunately the BCL does not have a Contains overload that allows you to specify case invariance. You will have to grab the correct overload of IndexOf as a workaround (checking to see if the result of IndexOf is greater than zero):

var methodInfo 
    = typeof(string)
        .GetMethod("IndexOf", 
            new[] { typeof(string), typeof(StringComparison) });

This MethodInfo accepts a string and a StringComparison which will allow you to specify StringComparison.OrdinalIgnoreCase if you wish.

这篇关于可以LINQ表达式不区分大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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