예제 #1
0
 public static QueryPredicate CombinePredicatesWithOR(IList<QueryPredicate> filters)
 {
     if(filters == null || filters.Count == 0)
     return null;
       if(filters.Count == 1)
     return filters[0];
       var allParams = new List<ParameterExpression>();
       var commonEntityParam = filters[0].Lambda.Parameters[0];
       allParams.Add(commonEntityParam);
       var rewriter = new FilterExpressionRewriter();
       //Build new body and new parameter list
       Expression newBody = null;
       foreach(var filter in filters) {
     var filterBody = rewriter.ReplaceParameter(filter.Lambda.Body, filter.Lambda.Parameters[0], commonEntityParam);
     if(newBody == null)
       newBody = filterBody;
     else
       newBody = Expression.Or(newBody, filterBody);
     allParams.AddRange(filter.Lambda.Parameters.Skip(1)); //first param is entity
       }
       var newLambda = Expression.Lambda(newBody, allParams.ToArray());
       var filterType = filters[0].GetType();
       var result = (QueryPredicate) Activator.CreateInstance(filterType, newLambda);
       return result;
 }
예제 #2
0
        public static QueryPredicate CombinePredicatesWithOR(IList <QueryPredicate> filters)
        {
            if (filters == null || filters.Count == 0)
            {
                return(null);
            }
            if (filters.Count == 1)
            {
                return(filters[0]);
            }
            var allParams         = new List <ParameterExpression>();
            var commonEntityParam = filters[0].Lambda.Parameters[0];

            allParams.Add(commonEntityParam);
            var rewriter = new FilterExpressionRewriter();
            //Build new body and new parameter list
            Expression newBody = null;

            foreach (var filter in filters)
            {
                var filterBody = rewriter.ReplaceParameter(filter.Lambda.Body, filter.Lambda.Parameters[0], commonEntityParam);
                if (newBody == null)
                {
                    newBody = filterBody;
                }
                else
                {
                    newBody = Expression.Or(newBody, filterBody);
                }
                allParams.AddRange(filter.Lambda.Parameters.Skip(1)); //first param is entity
            }
            var newLambda  = Expression.Lambda(newBody, allParams.ToArray());
            var filterType = filters[0].GetType();
            var result     = (QueryPredicate)Activator.CreateInstance(filterType, newLambda);

            return(result);
        }