未应用自定义lambda表达式

时间:2015-06-25 14:52:33

标签: c# lambda

我有一个方法,用于检查表中的列名列表中是否包含字符串列表。这是方法:

public static IQueryable<T> ContainsQuery<T>(IQueryable<T> query, IEnumerable<string> propertyNames, IEnumerable<string> propertyValues)
        {
            ParameterExpression entity = Expression.Parameter(typeof(T), "entity");
            MethodInfo containsInfo = typeof(string).GetMethod("Contains", new Type[] { typeof(string) });
            MethodInfo toLowerInfo = typeof(string).GetMethod("ToLower", new Type[] {});
            BinaryExpression masterExpression = Expression.Or(Expression.Constant(true, typeof(Boolean)), Expression.Constant(true, typeof(Boolean)));
            ConstantExpression negativeOne = Expression.Constant(-1, typeof(int));

            foreach (var propertyName in propertyNames)
            {
                foreach (var propertyValue in propertyValues)
                {
                    PropertyInfo propertyInfo = typeof(T).GetProperty(propertyName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
                    ConstantExpression valueExpression = Expression.Constant(propertyValue, typeof(string));
                    ConstantExpression caseInsensitiveComparisonExpression = Expression.Constant(StringComparison.CurrentCultureIgnoreCase, typeof(StringComparison));
                    MemberExpression propertyExpression = Expression.Property(entity, propertyInfo);
                    MethodCallExpression propertyLoweredExpression = Expression.Call(propertyExpression, toLowerInfo);
                    MethodCallExpression valueLoweredExpression = Expression.Call(valueExpression, toLowerInfo);
                    MethodCallExpression containsExpression = Expression.Call(propertyLoweredExpression, containsInfo, valueLoweredExpression);
                    masterExpression = Expression.Or(containsExpression, masterExpression); 
                }
            }
            Expression<Func<T, bool>> lambda = Expression.Lambda<Func<T, bool>>(masterExpression, entity);
            return query.Where(lambda);
        }

我正在使用它:

qry = QueryExpressions.ContainsQuery(qry, searchCols, new List<string> {universalSearch});

当我单步执行代码时,qry以IQueryable select all语句开头。然后调用此方法,我检查lambda,它是我期望的嵌套列表。当我走出该方法时,qry仍然像以前一样简单select all语句。我的lambda表达式没有被应用。无论我传递给universalSearch的是什么,qry都会返回整个数据集。

提前致谢。

方法执行完毕后,这是lambda:

{entity => (entity.STAKE_COMMENT.ToLower().Contains("other".ToLower()) Or (entity.NOTES.ToLower().Contains("other".ToLower()) Or (entity.COMMENT_SOURCE.ToLower().Contains("other".ToLower()) Or (entity.STAKE_COMMENT_DATE2.ToLower().Contains("other".ToLower()) Or (entity.COMMENT_STATUS.ToLower().Contains("other".ToLower()) Or (entity.COMMENT_CATEGORY.ToLower().Contains("other".ToLower()) Or (entity.COMMENTTOPIC.ToLower().Contains("other".ToLower()) Or (entity.STAKEHOLDER.ToLower().Contains("other".ToLower()) Or (True Or True)))))))))}

1 个答案:

答案 0 :(得分:2)

问题不在于您的查询未更改,而是您的谓词存在缺陷。

你有这个谓词:

... lots of stuff ... Or (True Or True)

包含or true的每个条件总是评估为true。