1

我正在尝试从表单中的谓词列表中构建一个“或”谓词List<Expression<Func<T, bool>>>

 public static IQueryable<T> Search<T>(this IQueryable<T> source, List<Expression<Func<T, bool>>> predicates = null)
            where T : EntityObject
        {
            if (predicates == null || predicates.Count == 0)
                return source;
            else if (predicates.Count == 1)
                return source.Where(predicates[0]);
            else
            {
                var row = Expression.Parameter(typeof(T), "row");
                var compoundExpression = predicates[0];

                for (int i = 1; i < predicates.Count; i++)
                {
                    compoundExpression = compoundExpression.Or(predicates[i]);
                }
                return source.Where(compoundExpression);
            }
        }

        static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> lhs, Expression<Func<T, bool>> rhs)
        {
            var row = Expression.Parameter(typeof(T), "row");
            var body = Expression.Or(
                Expression.Invoke(lhs, row),
                Expression.Invoke(rhs, row));
            return Expression.Lambda<Func<T, bool>>(body, row);
        }

但这会返回我的源代码中的每一行吗?

为了测试我正在寻找c=>c.FullName.Contains("Smith") or c=>c.FullName.Contains("Jones")

我尝试修改以使用PredicateBuilder但它仍然返回源中的每一行。

public static IQueryable<T> Search<T>(this IQueryable<T> source, List<Expression<Func<T, bool>>> predicates = null)
            where T : EntityObject
        {
            if (predicates == null || predicates.Count == 0)
                return source;
            else if (predicates.Count == 1)
                return source.Where(predicates[0]);
            else
            {
                var pb = PredicateBuilder.False<T>();
                for (int i = 0; i < predicates.Count; i++)
                {
                    pb = pb.Or(predicates[i]);
                }
                return source.AsExpandable().Where(pb);
            }
        }

非常感谢您的任何帮助!

最终结果将是允许 AND 和 OR,
例如 c=>c.FullName.Contains("Dav") AND c=>c.CustomerType == 'Staff'

4

2 回答 2

1

尝试

public static IQueryable<T> Search<T>(this IQueryable<T> source, IEnumerable<Expression<Func<T, bool>>> predicates = null)
        where T : EntityObject
    {
        if (predicates == null || !predicates.Any())
            return source;
        else
        {
             ParameterExpression p = Expression.Parameter(typeof(T), "p");
             Expression<Func<T,Bool>> predicate = 
                        Expression.Lambda<Func<T,Bool>(
                                   predicates.Select(l => ReParameteriser(l.Body, l.Paramaters[0], p)
                                             .Aggregate((b1,b2) => Expression.Or(b1,b2)),
                                   new ParamaterExpression[]{p});
            return source.Where(predicate);
        }
   }

public class ReParameteriser : ExpressionVisitor
{
    ParameterExpression originalParameter; 
    ParameterExpression newParameter;

    private ReParameteriser(){}
    protected ReParameteriser (ParameterExpression originalParameter, ParameterExpression newParameter) 
    {
         this.originalParameter = originalParameter;
         this.new = newParameter;
    }

    public static Expression ReParameterise(Expression expression, ParameterExpression originalParameter, ParameterExpression newParameter)
    {
        return new ReParameteriser(original,newParameter).Visit(expression);
    }

    protected override Expression VisitParameter(ParameterExpression node)
    {
        if (node == originalParameter)
            return newParameter;
        else
            return node;
    }
}

注意:ExpressionVisitor 类是 .Net4,因此如果您想针对更早的环境,您需要自己编写。这个代码只是谷歌,但通常的资源是马特沃伦的博客http://blogs.msdn.com/b/mattwar/archive/2008/11/18/linq-links.aspx

于 2012-11-08T13:58:37.520 回答
1

有时周末会做一些奇怪的事情来编码!

不知道我做了什么不同,但这是有效的:

public static IQueryable<T> Search<T>(this IQueryable<T> source, List<Expression<Func<T, bool>>> predicates = null)
            where T : EntityObject
        {
            if (predicates == null || predicates.Count == 0)
                return source;
            else if (predicates.Count == 1)
                return source.Where(predicates[0]);
            else
            {
                var query = PredicateBuilder.False<T>();
                foreach (var predicate in predicates)
                {
                    query = query.Or(predicate);
                }

                return source.AsExpandable().Where(query);
            }
        }

PredicateBuilder 似乎是一小段代码。

于 2012-11-11T06:26:14.677 回答