LINQ不区分大小写的查询

时间:2015-07-22 20:15:49

标签: c# linq

我继承了基于

的代码

https://gist.github.com/afreeland/6733381

 public class ExpressionBuilder
    {
        // Define some of our default filtering options
        private static MethodInfo containsMethod = typeof(string).GetMethod("Contains", new[] { typeof(string) });
        private static MethodInfo startsWithMethod = typeof(string).GetMethod("StartsWith", new[] { typeof(string) });
        private static MethodInfo endsWithMethod = typeof(string).GetMethod("EndsWith", new[] { typeof(string) });

        public static Expression<Func<T, bool>> GetExpression<T>(List<GridHelper.Filter> filters)
        {
                     // Odd number, handle this seperately
                    if (filters.Count == 1)
                    {
                        // Pass in our existing expression and our newly created expression from our last remaining filter
                        exp = Expression.AndAlso(exp, GetExpression<T>(param, filters[0]));

                        // Remove filter to break out of while loop
                        filters.RemoveAt(0);
                    }
                }
            }

            return Expression.Lambda<Func<T, bool>>(exp, param);
        }

        private static Expression GetExpression<T>(ParameterExpression param, GridHelper.Filter filter)
        {
            // The member you want to evaluate (x => x.FirstName)
            MemberExpression member = Expression.Property(param, filter.PropertyName);

            // The value you want to evaluate
            ConstantExpression constant = Expression.Constant(filter.Value);


            // Determine how we want to apply the expression
            switch (filter.Operator)
            {
                case GridHelper.Operator.Equals:
                    return Expression.Equal(member, constant);

            }

            return null;
        }

        private static BinaryExpression GetExpression<T>(ParameterExpression param, GridHelper.Filter filter1, GridHelper.Filter filter2)
        {
            Expression result1 = GetExpression<T>(param, filter1);
            Expression result2 = GetExpression<T>(param, filter2);
            return Expression.AndAlso(result1, result2);
        }


    }
public ActionResult Grid()
{
    //....

    //Implementing ExpressionBuilder
    Expression<Func<Contact, bool>> deleg = ExpressionBuilder.GetExpression<Contact>(filters);
    _contacts = _contacts.Where(deleg);

    //.....
}

// Filter class
public class GridHelper
{
     public enum  Operator
        {
            Contains,
            GreaterThan,
            GreaterThanOrEqual,
            LessThan,
            LessThanOrEqualTo,
            StartsWith,
            EndsWith,
            Equals,
            NotEqual
        }

        public class Filter
        {
            public string PropertyName { get; set; }
            public string Value { get; set; }
            private Operator _op = Operator.Contains;
            public Operator Operator
            {
                get
                {
                    return _op;
                }
                set
                {
                    _op = value;
                }
            }
        }



    }
    }

但是我想使equals操作不敏感。

我尝试了线程can linq expression be case insensitive

中提到的扩展类

但我得到以下异常

“类型'System.Linq.Expressions.ConstantExpression'的表达式不能用于方法'Boolean ContainsExt(System.String,System.String)''}}

方法'System.String'类型的参数 你会的吗?让我知道如何处理这个问题?

由于

0 个答案:

没有答案