internal static EvaluationResult TryBuildPredicate(this Filter filter, Type type, BuildArgument arg)
        {
            var isInvalid = !arg.IsValidProperty(filter.Property);

            isInvalid |= !arg.IsValidOperator(filter.Operator);
            if (isInvalid)
            {
                return(new EvaluationResult
                {
                    InvalidProperties = arg.InvalidProperties,
                    InvalidOperators = arg.InvalidOperators
                });
            }

            try
            {
                var expression = Interpreter.BuildPredicate(filter.Operator.GetComparisonOperator(), arg.MapProperty(filter.Property), filter.Value, type, arg.VariableResolver);

                return(new EvaluationResult
                {
                    Result = expression,
                    Succeeded = true
                });
            }
            catch (Exception ex)
            {
                return(new EvaluationResult
                {
                    Exception = ex
                });
            }
        }
Example #2
0
        internal static EvaluationResult <T, bool> TryBuildPredicate <T>(this FilterGroup filterGroup, BuildArgument arg)
        {
            filterGroup.Filters.ForEach(f => { arg.IsValidProperty(f.Property); arg.IsValidOperator(f.Operator); });
            if (arg.InvalidProperties.Any() || arg.InvalidOperators.Any())
            {
                return(new EvaluationResult <T, bool>
                {
                    InvalidProperties = arg.InvalidProperties,
                    InvalidOperators = arg.InvalidOperators
                });
            }

            try
            {
                var expression = (Expression <Func <T, bool> >)FilterExtensions.BuildPredicateInternal(filterGroup.Filters, typeof(T), arg);

                return(new EvaluationResult <T, bool>
                {
                    Result = expression,
                    Succeeded = true
                });
            }
            catch (Exception ex)
            {
                return(new EvaluationResult <T, bool>
                {
                    Exception = ex
                });
            }
        }
        internal static EvaluationResult TryBuildPredicate(this IEnumerable <Filter> filters, Type type, BuildArgument arg)
        {
            if (filters == null)
            {
                throw new ArgumentNullException(nameof(filters));
            }
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            if (!filters.Any())
            {
                return new EvaluationResult
                       {
                           Succeeded = true,
                           Result    = AlwaysTruePredicate(type)
                       }
            }
            ;

            var allFilters = filters as Filter[] ?? filters.ToArray();

            foreach (var filter in allFilters)
            {
                arg.IsValidProperty(filter?.Property);

                arg.IsValidOperator(filter?.Operator);
            }

            if (arg.InvalidOperators.Any() || arg.InvalidProperties.Any())
            {
                return(new EvaluationResult
                {
                    InvalidProperties = arg.InvalidProperties,
                    InvalidOperators = arg.InvalidOperators
                });
            }

            try
            {
                var param = type.CreateParameterExpression();
                var body  = BuildBody(allFilters, param, arg);
                return(new EvaluationResult
                {
                    Succeeded = true,
                    Result = Expression.Lambda(body, param)
                });
            }
            catch (Exception ex)
            {
                return(new EvaluationResult
                {
                    Exception = ex
                });
            }
        }
Example #4
0
        internal static EvaluationResult TryBuildPredicate(this IEnumerable <FilterGroup> filterGroups, Type type, BuildArgument arg)
        {
            if (!filterGroups.Any())
            {
                return new EvaluationResult
                       {
                           Succeeded = true,
                           Result    = FilterExtensions.AlwaysTruePredicate(type)
                       }
            }
            ;

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

            foreach (var filterGroup in allFilterGroups)
            {
                if (filterGroup is null)
                {
                    throw new InvalidOperationException("FilterGroup can not be null");
                }
                if (filterGroup.Filters is null)
                {
                    throw new InvalidOperationException("Filters of FilterGroup can not be null");
                }

                foreach (var filter in filterGroup.Filters)
                {
                    arg.IsValidProperty(filter.Property);
                    arg.IsValidOperator(filter.Operator);
                }
            }

            if (arg.InvalidOperators.Any() || arg.InvalidProperties.Any())
            {
                return(new EvaluationResult
                {
                    InvalidProperties = arg.InvalidProperties,
                    InvalidOperators = arg.InvalidOperators
                });
            }

            try
            {
                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(new EvaluationResult
                {
                    Succeeded = true,
                    Result = Expression.Lambda(body, param)
                });
            }
            catch (Exception ex)
            {
                return(new EvaluationResult
                {
                    Exception = ex
                });
            }
        }