Represents declarative filtering.
Inheritance: FilterDescriptorBase
        protected virtual Expression VisitBinary(FilterDescriptor filter)
        {
            Expression property =
                LinqHelpers.GetPropertyExpression(Parameter, filter.Member);

            switch (filter.Operator)
            {
                case FilterOperator.Contains:
                    return VisitContains(filter, property);
                case FilterOperator.StartsWith:
                    return VisitStartsWith(filter, property);
                case FilterOperator.EndsWith:
                    return VisitEndsWith(filter, property);
                case FilterOperator.IsEqualTo:
                    return VisitIsEqualTo(filter, property);
                case FilterOperator.IsNotEqualTo:
                    return VisitIsNotEqualTo(filter, property);
                case FilterOperator.IsContainedIn:
                    return VisitIsContainedIn(filter, property);
                case FilterOperator.IsGreaterThan:
                    return VisitIsGreaterThan(filter, property);
                case FilterOperator.IsGreaterThanOrEqualTo:
                    return VisitIsGreaterThanOrEqualTo(filter, property);
                case FilterOperator.IsLessThan:
                    return VisitIsLessThan(filter, property);
                case FilterOperator.IsLessThanOrEqualTo:
                    return VisitIsLessThanOrEqualTo(filter, property);
                default:
                    return Expression.Constant(true);
            }
        }
        public void CanCreateSampleEqualsFilterToIntNullable()
        {
            var filter = new FilterDescriptor("PropIntNullable", FilterOperator.IsEqualTo, 42);

            var expr = new FilterVisitor<ClassA>().MakePredicate(filter);

            Assert.AreEqual("x => (x.PropIntNullable = 42)", expr.ToString());
        }
        public void CanCreateSampleContainsFilterWithNullValue()
        {
            var filter = new FilterDescriptor("PropString", FilterOperator.Contains, null);

            var expr = new FilterVisitor<ClassA>().MakePredicate(filter);

            Assert.AreEqual("x => x.PropString.ToUpper().Contains(null)", expr.ToString());
        }
        public void CanCreateSampleEndsWithFilter()
        {
            var filter = new FilterDescriptor("PropString", FilterOperator.EndsWith, "asd");

            var expr = new FilterVisitor<ClassA>().MakePredicate(filter);

            Assert.AreEqual("x => x.PropString.ToUpper().EndsWith(\"ASD\")", expr.ToString());
        }
        /// <summary>
        /// Determines whether the specified <paramref name="other"/> descriptor
        /// is equal to the current one.
        /// </summary>
        /// <param name="other">The other filter descriptor.</param>
        /// <returns>
        /// True if all members of the current descriptor are
        /// equal to the ones of <paramref name="other"/>, otherwise false.
        /// </returns>
        public virtual bool Equals(FilterDescriptor other)
        {
            if (ReferenceEquals(null, other))
            {
                return(false);
            }
            if (ReferenceEquals(this, other))
            {
                return(true);
            }

            return
                (Equals(other.Operator, this.Operator) &&
                 Equals(other.Member, this.Member) &&
                 Equals(other.Value, this.Value));
        }
 protected virtual Expression VisitStartsWith(FilterDescriptor filter, Expression property)
 {
     return VisitStringMethod(filter, property, "StartsWith");
 }
        protected virtual Expression VisitIsNotEqualTo(FilterDescriptor filter, Expression property)
        {
            Expression expr = property;
            object value = filter.Value;

            CheckStringParameters(property, ref expr, ref value);

            return Expression.NotEqual(expr, VisitConstantValue(value, property));
        }
 protected virtual Expression VisitIsLessThanOrEqualTo(FilterDescriptor filter, Expression property)
 {
     return Expression.LessThanOrEqual(property, VisitConstantValue(filter.Value, property));
 }
 protected virtual Expression VisitIsGreaterThan(FilterDescriptor filter, Expression property)
 {
     return Expression.GreaterThan(property, VisitConstantValue(filter.Value, property));
 }
 protected virtual Expression VisitIsContainedIn(FilterDescriptor filter, Expression property)
 {
     return VisitEnumerableMethod(filter, property, LinqHelpers.Contains.GetFor(property.Type));
 }
        public void CanCreateSampleGreaterFilterToClassBWithDoubleValue()
        {
            var filter = new FilterDescriptor("PropB.PropDecimalNullable", FilterOperator.IsGreaterThan, 42.0);

            var expr = new FilterVisitor<ClassA>().MakePredicate(filter);

            Assert.AreEqual("x => (x.PropB.PropDecimalNullable > 42)", expr.ToString());
        }
        public void CanCreateSampleNotEqualsToFilter()
        {
            var filter = new FilterDescriptor("PropString", FilterOperator.IsNotEqualTo, "asd");

            var expr = new FilterVisitor<ClassA>().MakePredicate(filter);

            Assert.AreEqual("x => (x.PropString.ToUpper() != \"ASD\")", expr.ToString());
        }
        public void CanCreateSampleLessFilterToClassBDateTimeNullable()
        {
            var now = DateTime.Now;
            var filter = new FilterDescriptor("PropB.PropDateTimeNullable", FilterOperator.IsLessThan, now);

            var expr = new FilterVisitor<ClassA>().MakePredicate(filter);

            Assert.AreEqual(string.Format("x => (x.PropB.PropDateTimeNullable < {0})", now.ToString()), expr.ToString());
        }
        public void CanCreateSampleIsContainedInFilterWithIntList()
        {
            var list = new[] { 1, 2, 3 };
            var filter = new FilterDescriptor("PropInt", FilterOperator.IsContainedIn, list);

            Expression<Func<int, bool>> ex = x => list.Contains(x);

            var expr = new FilterVisitor<ClassA>().MakePredicate(filter);

            Assert.AreEqual("x => value(System.Int32[]).Contains(x.PropInt)", expr.ToString());
        }
        protected virtual Expression VisitStringMethod(FilterDescriptor filter, Expression property, string methodName)
        {
            var value = NormalizeStringParameter(filter.Value);
            var expr = NormalizeStringExpression(property);

            var method = typeof(string).GetMethod(methodName, new Type[] { typeof(string) });
            var parameters = new[] {
                Expression.Constant(value, typeof(string))
            };

            return Expression.Call(expr, method, parameters);
        }
 protected virtual Expression VisitContains(FilterDescriptor filter, Expression property)
 {
     return VisitStringMethod(filter, property, "Contains");
 }
 private Expression VisitIsGreaterThanOrEqualTo(FilterDescriptor filter, Expression property)
 {
     return Expression.GreaterThanOrEqual(property, VisitConstantValue(filter.Value, property));
 }
        protected virtual Expression VisitEnumerableMethod(FilterDescriptor filter, Expression property, MethodInfo method)
        {
            var expr = Expression.Constant(filter.Value, typeof(IEnumerable<>).MakeGenericType(property.Type));
            var parameters = new[] { expr, property };

            return Expression.Call(null, method, parameters);
        }
        /// <summary>
        /// Determines whether the specified <paramref name="other"/> descriptor 
        /// is equal to the current one.
        /// </summary>
        /// <param name="other">The other filter descriptor.</param>
        /// <returns>
        /// True if all members of the current descriptor are 
        /// equal to the ones of <paramref name="other"/>, otherwise false.
        /// </returns>
        public virtual bool Equals(FilterDescriptor other)
        {
            if (ReferenceEquals(null, other))
            {
                return false;
            }
            if (ReferenceEquals(this, other))
            {
                return true;
            }

            return
                Equals(other.Operator, this.Operator) &&
                Equals(other.Member, this.Member) &&
                Equals(other.Value, this.Value);
        }
        public void CanCreateSampleEqualsFilterToIntWithQueryableVersion()
        {
            var filter = new FilterDescriptor("PropInt", FilterOperator.IsEqualTo, 42);
            var param = Expression.Parameter(typeof(IQueryable<ClassA>), "q");
            var expr = new FilterVisitor<ClassA>().Visit(param, filter);

            Assert.AreEqual("q.Where(x => (x.PropInt = 42))", expr.ToString());
        }