Represents declarative filtering.
Inheritance: FilterDescriptorBase
コード例 #1
0
        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);
            }
        }
コード例 #2
0
        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());
        }
コード例 #3
0
        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());
        }
コード例 #4
0
        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());
        }
コード例 #5
0
        /// <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));
        }
コード例 #6
0
 protected virtual Expression VisitStartsWith(FilterDescriptor filter, Expression property)
 {
     return VisitStringMethod(filter, property, "StartsWith");
 }
コード例 #7
0
        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));
        }
コード例 #8
0
 protected virtual Expression VisitIsLessThanOrEqualTo(FilterDescriptor filter, Expression property)
 {
     return Expression.LessThanOrEqual(property, VisitConstantValue(filter.Value, property));
 }
コード例 #9
0
 protected virtual Expression VisitIsGreaterThan(FilterDescriptor filter, Expression property)
 {
     return Expression.GreaterThan(property, VisitConstantValue(filter.Value, property));
 }
コード例 #10
0
 protected virtual Expression VisitIsContainedIn(FilterDescriptor filter, Expression property)
 {
     return VisitEnumerableMethod(filter, property, LinqHelpers.Contains.GetFor(property.Type));
 }
コード例 #11
0
        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());
        }
コード例 #12
0
        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());
        }
コード例 #13
0
        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());
        }
コード例 #14
0
        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());
        }
コード例 #15
0
        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);
        }
コード例 #16
0
 protected virtual Expression VisitContains(FilterDescriptor filter, Expression property)
 {
     return VisitStringMethod(filter, property, "Contains");
 }
コード例 #17
0
 private Expression VisitIsGreaterThanOrEqualTo(FilterDescriptor filter, Expression property)
 {
     return Expression.GreaterThanOrEqual(property, VisitConstantValue(filter.Value, property));
 }
コード例 #18
0
        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);
        }
コード例 #19
0
        /// <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);
        }
コード例 #20
0
        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());
        }