public static LambdaExpression BuildPredicate(this Filter filter, Type type, VariableResolver variableResolver = null, IEnumerable <string> validProperties = null, IDictionary <string, string> propertyMapping = null)
        {
            if (filter == null)
            {
                throw new ArgumentNullException(nameof(filter));
            }
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            var arg = new BuildArgument
            {
                ValidProperties  = validProperties,
                EvaluationType   = type,
                VariableResolver = variableResolver,
                PropertyMapping  = propertyMapping
            };

            if (!IsValid(filter, arg))
            {
                throw new InvalidOperationException("The operator or property name is not valid");
            }

            return(Interpreter.BuildPredicate(filter.Operator.GetComparisonOperator(), arg.MapProperty(filter.Property), filter.Value, type, arg.VariableResolver));
        }
        public static EvaluationResult <T, bool> TryBuildPredicate <T>(this IEnumerable <Filter> filters, VariableResolver variableResolver = null, IEnumerable <string> validProperties = null, IDictionary <string, string> propertyMapping = null)
        {
            if (filters == null)
            {
                throw new ArgumentNullException(nameof(filters));
            }
            var result = TryBuildPredicate(filters, typeof(T), variableResolver, validProperties, propertyMapping);

            return(new EvaluationResult <T, bool>
            {
                Exception = result.Exception,
                InvalidOperators = result.InvalidOperators,
                InvalidProperties = result.InvalidProperties,
                InvalidValues = result.InvalidValues,
                InvalidVariables = result.InvalidVariables,
                Result = (Expression <Func <T, bool> >)result.Result,
                Succeeded = result.Succeeded
            });
        }
        public static LambdaExpression BuildPredicate(this IEnumerable <Filter> filters, Type type, VariableResolver variableResolver = null, IEnumerable <string> validProperties = null, IDictionary <string, string> propertyMapping = null)
        {
            if (filters == null)
            {
                throw new ArgumentNullException(nameof(filters));
            }
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            var arg = new BuildArgument
            {
                ValidProperties  = validProperties,
                EvaluationType   = type,
                VariableResolver = variableResolver,
                PropertyMapping  = propertyMapping
            };

            return(BuildPredicateInternal(filters, type, arg));
        }
 public static Expression <Func <T, bool> > BuildPredicate <T>(this IEnumerable <Filter> filters, VariableResolver variableResolver = null, IEnumerable <string> validProperties = null, IDictionary <string, string> propertyMapping = null)
 {
     return((Expression <Func <T, bool> >)BuildPredicate(filters, typeof(T), variableResolver, validProperties, propertyMapping));
 }
        public static EvaluationResult <T, bool> ParsePredicate <T>(this string expression, VariableResolver variableResolver = null, IEnumerable <string> validProperties = null, IDictionary <string, string> propertyMapping = null)
        {
            if (string.IsNullOrWhiteSpace(expression))
            {
                throw new ArgumentException("Invalid expression", nameof(expression));
            }

            var type   = typeof(T);
            var result = ExpressionParser.Parse(expression, type, new BuildArgument
            {
                ValidProperties  = validProperties,
                EvaluationType   = type,
                VariableResolver = variableResolver,
                PropertyMapping  = propertyMapping
            });

            return(new EvaluationResult <T, bool>
            {
                Result = (Expression <Func <T, bool> >)result.Result,
                Exception = result.Exception,
                InvalidProperties = result.InvalidProperties,
                InvalidVariables = result.InvalidVariables,
                InvalidOperators = result.InvalidOperators,
                Succeeded = result.Succeeded
            });
        }
        public static EvaluationResult TryBuildPredicate(this Filter filter, Type type, VariableResolver variableResolver = null, IEnumerable <string> validProperties = null, IDictionary <string, string> propertyMapping = null)
        {
            if (filter == null)
            {
                throw new ArgumentNullException(nameof(filter));
            }
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            var arg = new BuildArgument
            {
                ValidProperties  = validProperties,
                EvaluationType   = type,
                VariableResolver = variableResolver,
                PropertyMapping  = propertyMapping
            };

            return(TryBuildPredicate(filter, type, arg));
        }
        internal static Expression BuildBody(ComparisonOperator @operator, MemberExpression prop, object value, VariableResolver variableResolver)
        {
            if (value is string && prop.Type != typeof(string) && @operator != ComparisonOperator.In)
            {
                var varName = (string)value;
                if (variableResolver.IsVariable(varName) && variableResolver.TryResolve(varName, out var result))
                {
                    value = Convert.ChangeType(result, prop.Type);
                }
                else
                {
                    value = varName.Cast(prop.Type);
                }
            }

            @operator = CorrectOperator(prop.Type, @operator);
            switch (@operator)
            {
            case ComparisonOperator.NotEqual:
                return(Expression.NotEqual(prop, Expression.Constant(value, prop.Type)));

            case ComparisonOperator.LessThan:
                return(Expression.LessThan(prop, Expression.Constant(value, prop.Type)));

            case ComparisonOperator.LessThanOrEqual:
                return(Expression.LessThanOrEqual(prop, Expression.Constant(value, prop.Type)));

            case ComparisonOperator.GreaterThan:
                return(Expression.GreaterThan(prop, Expression.Constant(value, prop.Type)));

            case ComparisonOperator.GreaterThanOrEqual:
                return(Expression.GreaterThanOrEqual(prop, Expression.Constant(value, prop.Type)));

            case ComparisonOperator.Contains:
                return(Expression.Call(prop, StringContainsMethod, Expression.Constant(value, prop.Type)));

            case ComparisonOperator.StartsWith:
                return(Expression.Call(prop, StringStartsWithMethod, Expression.Constant(value, prop.Type)));

            case ComparisonOperator.EndsWith:
                return(Expression.Call(prop, StringEndsWithMethod, Expression.Constant(value, prop.Type)));

            case ComparisonOperator.In:
                return(Expression.Call(typeof(Enumerable), nameof(Enumerable.Contains), new Type[] { prop.Type }, new ArrayList {
                    Type = prop.Type, DrawValue = value.ToString()
                }.Build(null), prop));

            default:
                return(Expression.Equal(prop, Expression.Constant(value, prop.Type)));
            }
        }
Beispiel #8
0
        public static LambdaExpression BuildPredicate(this FilterGroup filterGroup, Type type, VariableResolver variableResolver = null, IEnumerable <string> validProperties = null, IDictionary <string, string> propertyMapping = null)
        {
            if (filterGroup == null)
            {
                throw new ArgumentNullException(nameof(filterGroup));
            }
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            var arg = new BuildArgument
            {
                ValidProperties  = validProperties,
                EvaluationType   = type,
                VariableResolver = variableResolver,
                PropertyMapping  = propertyMapping
            };

            if (!IsValid(filterGroup, arg))
            {
                throw new InvalidOperationException("The operator or property name is not valid");
            }

            return(FilterExtensions.BuildPredicateInternal(filterGroup.Filters, type, arg));
        }
 public static Expression <Func <TEntity, bool> > BuildPredicate <TEntity>(string propertyName, ComparisonOperator @operator, object value, VariableResolver variableResolver = null)
 {
     if (string.IsNullOrWhiteSpace(propertyName))
     {
         throw new ArgumentException($"{nameof(propertyName)} is required", nameof(propertyName));
     }
     return((Expression <Func <TEntity, bool> >)BuildPredicate(@operator, propertyName, value, typeof(TEntity), variableResolver ?? new VariableResolver()));
 }
Beispiel #10
0
        public static LambdaExpression BuildPredicate(string propertyName, ComparisonOperator @operator, object value, Type type, VariableResolver variableResolver = null)
        {
            if (string.IsNullOrWhiteSpace(propertyName))
            {
                throw new ArgumentException($"{nameof(propertyName)} is required", nameof(propertyName));
            }
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            return(BuildPredicate(@operator, propertyName, value, type, variableResolver ?? new VariableResolver()));
        }
Beispiel #11
0
        public static EvaluationResult ParsePredicate(this string expression, Type type, VariableResolver variableResolver = null, IEnumerable <string> validProperties = null, IDictionary <string, string> propertyMapping = null)
        {
            if (string.IsNullOrWhiteSpace(expression))
            {
                throw new ArgumentException($"{nameof(expression)} is required", nameof(expression));
            }
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            return(ExpressionParser.Parse(expression, type, new BuildArgument
            {
                ValidProperties = validProperties,
                EvaluationType = type,
                VariableResolver = variableResolver,
                PropertyMapping = propertyMapping
            }));
        }
Beispiel #12
0
        public static EvaluationResult <T, bool> TryBuildPredicate <T>(this FilterGroup filterGroup, VariableResolver variableResolver = null, IEnumerable <string> validProperties = null, IDictionary <string, string> propertyMapping = null)
        {
            if (filterGroup == null)
            {
                throw new ArgumentNullException(nameof(filterGroup));
            }
            ;
            var arg = new BuildArgument
            {
                ValidProperties  = validProperties,
                EvaluationType   = typeof(T),
                VariableResolver = variableResolver,
                PropertyMapping  = propertyMapping
            };

            return(TryBuildPredicate <T>(filterGroup, arg));
        }
Beispiel #13
0
        public static LambdaExpression BuildPredicate(this IEnumerable <FilterGroup> filterGroups, Type type, VariableResolver variableResolver = null, IEnumerable <string> validProperties = null, IDictionary <string, string> propertyMapping = null)
        {
            if (filterGroups == null)
            {
                throw new ArgumentNullException(nameof(filterGroups));
            }
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            var arg = new BuildArgument
            {
                ValidProperties  = validProperties,
                EvaluationType   = type,
                VariableResolver = variableResolver,
                PropertyMapping  = propertyMapping
            };

            if (!IsValid(filterGroups, arg))
            {
                throw new InvalidOperationException("The operator or property name is not valid");
            }

            var allFilterGroups = filterGroups as FilterGroup[] ?? filterGroups.ToArray();

            allFilterGroups = allFilterGroups.Where(p => p?.Filters?.Any() == true).ToArray();
            if (!allFilterGroups.Any())
            {
                return(FilterExtensions.AlwaysTruePredicate(type));
            }

            var param = type.CreateParameterExpression();
            var body  = allFilterGroups.Aggregate((Expression)null, (current, next) => current == null
                                ? FilterExtensions.BuildBody(next.Filters.ToArray(), param, arg)
                                : Expression.OrElse(current, FilterExtensions.BuildBody(next.Filters.ToArray(), param, arg)));

            return(Expression.Lambda(body, param));
        }
 public virtual Condition <T> BuildCondition <T>(VariableResolver variableResolver = null, IEnumerable <string> validProperties = null, IDictionary <string, string> propertyMapping = null)
 {
     return((Condition <T>)BuildCondition(typeof(T), variableResolver, validProperties, propertyMapping));
 }
Beispiel #15
0
        internal static LambdaExpression BuildPredicate(ComparisonOperator @operator, string propertyName, object value, Type type, VariableResolver variableResolver)
        {
            var param = type.CreateParameterExpression();
            var prop  = param.CreatePropertyExpression(propertyName);
            var body  = BuildBody(@operator, prop, value, variableResolver);

            return(Expression.Lambda(body, param));
        }
        public virtual Condition BuildCondition(Type type, VariableResolver variableResolver = null, IEnumerable <string> validProperties = null, IDictionary <string, string> propertyMapping = null)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            var arg = new BuildArgument
            {
                ValidProperties  = validProperties,
                VariableResolver = variableResolver,
                EvaluationType   = type,
                PropertyMapping  = propertyMapping
            };

            var           error         = new ConditionBase.ErrorInfo();
            var           result        = new Condition();
            var           predicates    = new List <LambdaExpression>();
            var           exceptions    = new List <Exception>();
            OrderByClause orderByClause = null;

            if (Filters != null && Filters.Any())
            {
                var filtersResult = Filters.TryBuildPredicate(type, arg);
                if (filtersResult.Succeeded)
                {
                    predicates.Add(filtersResult.Result);
                }
                else
                {
                    if (filtersResult.Exception != null)
                    {
                        exceptions.Add(filtersResult.Exception);
                    }
                }
            }

            if (FilterGroups != null && FilterGroups.Any())
            {
                var filterGroupsResult = FilterGroups.TryBuildPredicate(type, arg);
                if (filterGroupsResult.Succeeded)
                {
                    predicates.Add(filterGroupsResult.Result);
                }
                else
                {
                    if (filterGroupsResult.Exception != null)
                    {
                        exceptions.Add(filterGroupsResult.Exception);
                    }
                }
            }

            if (!string.IsNullOrEmpty(Where))
            {
                var whereResult = ExpressionParser.Parse(Where, type, arg);
                if (whereResult.Succeeded)
                {
                    predicates.Add(whereResult.Result);
                }
                else
                {
                    if (whereResult.Exception != null)
                    {
                        exceptions.Add(whereResult.Exception);
                    }
                }
            }

            if (!string.IsNullOrWhiteSpace(OrderBy))
            {
                var orderByResult = OrderByParser.Parse(OrderBy, arg);
                if (orderByResult.Succeeded)
                {
                    orderByClause = orderByResult.Result;
                }
                else
                {
                    if (orderByResult.Exception != null)
                    {
                        exceptions.Add(orderByResult.Exception);
                    }
                }
            }
            else if (OrderBys != null && OrderBys.Any())
            {
                var orderBysResult = OrderByParser.Parse(OrderBys.ToArray(), arg);
                if (orderBysResult.Succeeded)
                {
                    orderByClause = orderBysResult.Result;
                }
                else
                {
                    if (orderBysResult.Exception != null)
                    {
                        exceptions.Add(orderBysResult.Exception);
                    }
                }
            }

            var isInvalid = arg.InvalidProperties.Any() || arg.InvalidOperators.Any() || arg.InvalidVariables.Any() || exceptions.Any();

            result.IsValid = !isInvalid;
            if (isInvalid)
            {
                error.EvaluationResult = new EvaluationResultBase
                {
                    InvalidProperties        = arg.InvalidProperties,
                    InvalidValues            = arg.InvalidValues,
                    InvalidOperators         = arg.InvalidOperators,
                    InvalidVariables         = arg.InvalidVariables,
                    InvalidOrderByDirections = arg.InvalidOrderByDirections
                };
                error.Exceptions = exceptions;
                result.Error     = error;
            }
            else
            {
                result.Predicates    = predicates;
                result.OrderByClause = orderByClause;
            }

            return(result);
        }
Beispiel #17
0
 public static Expression <Func <T, bool> > BuildPredicate <T>(this FilterGroup filterGroup, VariableResolver variableResolver = null, IEnumerable <string> validProperties = null, IDictionary <string, string> propertyMapping = null)
 {
     if (filterGroup == null)
     {
         throw new ArgumentNullException(nameof(filterGroup));
     }
     return((Expression <Func <T, bool> >)FilterExtensions.BuildPredicate(filterGroup.Filters, typeof(T), variableResolver, validProperties, propertyMapping));
 }