static OperatorComparisonAttribute()
 {
     Equal              = new OperatorComparisonAttribute(OperatorType.Equal);
     NotEqual           = new OperatorComparisonAttribute(OperatorType.NotEqual);
     GreaterThan        = new OperatorComparisonAttribute(OperatorType.GreaterThan);
     GreaterThanOrEqual = new OperatorComparisonAttribute(OperatorType.GreaterThanOrEqual);
     LessThan           = new OperatorComparisonAttribute(OperatorType.LessThan);
     LessThanOrEqual    = new OperatorComparisonAttribute(OperatorType.LessThanOrEqual);
 }
Exemplo n.º 2
0
        public Expression BuildExpression(Type entityType, Expression body)
        {
            Expression finalExpression = body;

            foreach (var key in this.Keys)
            {
                var filterValue = new DynamicFilter(this[key]);
                if (IsPrimitive(key))
                {
                    var targetProperty = entityType.GetProperty(key);
                    var value          = Convert.ChangeType((string)filterValue, targetProperty.PropertyType);
                    var exp            = new OperatorComparisonAttribute(OperatorType.Equal).BuildExpression(body, targetProperty, filterProperty: null, value);

                    var combined = finalExpression.Combine(exp, CombineWith);
                    finalExpression = body.Combine(combined, CombineWith);
                }
                else
                {
                    var splitted = key.Split('.');
                    if (IsNotInnerObject(splitted))
                    {
                        var propName          = splitted[0];
                        var targetProperty    = entityType.GetProperty(propName);
                        var value             = Convert.ChangeType((string)filterValue, targetProperty.PropertyType);
                        var comparisonKeyword = splitted[1];
                        if (specialKeywords.TryGetValue(comparisonKeyword, out IFilterableType filterable))
                        {
                            var exp = filterable.BuildExpression(body, targetProperty, filterProperty: null, value);

                            var combined = finalExpression.Combine(exp, CombineWith);
                            finalExpression = body.Combine(combined, CombineWith);
                        }
                    }
                    else
                    {
                        throw new NotImplementedException("Inner objects are not supported yet!");
                    }
                }
            }

            return(finalExpression);

            bool IsNotInnerObject(string[] splitted) => splitted.Length == 2;
        }