LessThanOrEqual() public static method

Creates a BinaryExpression that represents a "less than or equal" numeric comparison.
public static LessThanOrEqual ( Expression left, Expression right ) : BinaryExpression
left Expression An to set the property equal to.
right Expression An to set the property equal to.
return BinaryExpression
Ejemplo n.º 1
0
        public void TestWhereComparison()
        {
            // Partial LINQ expression (x => x.Number1)
            var parameter = LinqExpression.Parameter(typeof(NumbersModel), "x");
            var n1        = LinqExpression.Property(parameter, "Number1");

            var l3    = new Func <int, bool>(n => n < 3);
            var le3   = new Func <int, bool>(n => n <= 3);
            var g6    = new Func <int, bool>(n => n > 6);
            var ge6   = new Func <int, bool>(n => n >= 6);
            var e7    = new Func <int, bool>(n => n == 7);
            var ne7   = new Func <int, bool>(n => n != 7);
            var cases = new[] {
                Tuple.Create((LinqExpression)LinqExpression.LessThan(n1, LinqExpression.Constant(3)),
                             (Func <NumbersModel, object, bool>)TestWhereCompareValidator, (object)l3, parameter),
                Tuple.Create((LinqExpression)LinqExpression.LessThanOrEqual(n1, LinqExpression.Constant(3)),
                             (Func <NumbersModel, object, bool>)TestWhereCompareValidator, (object)le3, parameter),
                Tuple.Create((LinqExpression)LinqExpression.GreaterThan(n1, LinqExpression.Constant(6)),
                             (Func <NumbersModel, object, bool>)TestWhereCompareValidator, (object)g6, parameter),
                Tuple.Create((LinqExpression)LinqExpression.GreaterThanOrEqual(n1, LinqExpression.Constant(6)),
                             (Func <NumbersModel, object, bool>)TestWhereCompareValidator, (object)ge6, parameter),
                Tuple.Create((LinqExpression)LinqExpression.Equal(n1, LinqExpression.Constant(7)),
                             (Func <NumbersModel, object, bool>)TestWhereCompareValidator, (object)e7, parameter),
                Tuple.Create((LinqExpression)LinqExpression.NotEqual(n1, LinqExpression.Constant(7)),
                             (Func <NumbersModel, object, bool>)TestWhereCompareValidator, (object)ne7, parameter)
            };

            LoadModelNumbers(10);
            Db.Count.Should().Be(10);
            RunTestWithNumbers(new[] { 2, 3, 4, 5, 1, 9 }, cases);
        }
        private BinaryExpression BinaryExpression(
            ExpressionType nodeType, System.Type type, JObject obj)
        {
            var left       = this.Prop(obj, "left", this.Expression);
            var right      = this.Prop(obj, "right", this.Expression);
            var method     = this.Prop(obj, "method", this.Method);
            var conversion = this.Prop(obj, "conversion", this.LambdaExpression);
            var liftToNull = this.Prop(obj, "liftToNull").Value <bool>();

            switch (nodeType)
            {
            case ExpressionType.Add: return(Expr.Add(left, right, method));

            case ExpressionType.AddAssign: return(Expr.AddAssign(left, right, method, conversion));

            case ExpressionType.AddAssignChecked: return(Expr.AddAssignChecked(left, right, method, conversion));

            case ExpressionType.AddChecked: return(Expr.AddChecked(left, right, method));

            case ExpressionType.And: return(Expr.And(left, right, method));

            case ExpressionType.AndAlso: return(Expr.AndAlso(left, right, method));

            case ExpressionType.AndAssign: return(Expr.AndAssign(left, right, method, conversion));

            case ExpressionType.ArrayIndex: return(Expr.ArrayIndex(left, right));

            case ExpressionType.Assign: return(Expr.Assign(left, right));

            case ExpressionType.Coalesce: return(Expr.Coalesce(left, right, conversion));

            case ExpressionType.Divide: return(Expr.Divide(left, right, method));

            case ExpressionType.DivideAssign: return(Expr.DivideAssign(left, right, method, conversion));

            case ExpressionType.Equal: return(Expr.Equal(left, right, liftToNull, method));

            case ExpressionType.ExclusiveOr: return(Expr.ExclusiveOr(left, right, method));

            case ExpressionType.ExclusiveOrAssign: return(Expr.ExclusiveOrAssign(left, right, method, conversion));

            case ExpressionType.GreaterThan: return(Expr.GreaterThan(left, right, liftToNull, method));

            case ExpressionType.GreaterThanOrEqual: return(Expr.GreaterThanOrEqual(left, right, liftToNull, method));

            case ExpressionType.LeftShift: return(Expr.LeftShift(left, right, method));

            case ExpressionType.LeftShiftAssign: return(Expr.LeftShiftAssign(left, right, method, conversion));

            case ExpressionType.LessThan: return(Expr.LessThan(left, right, liftToNull, method));

            case ExpressionType.LessThanOrEqual: return(Expr.LessThanOrEqual(left, right, liftToNull, method));

            case ExpressionType.Modulo: return(Expr.Modulo(left, right, method));

            case ExpressionType.ModuloAssign: return(Expr.ModuloAssign(left, right, method, conversion));

            case ExpressionType.Multiply: return(Expr.Multiply(left, right, method));

            case ExpressionType.MultiplyAssign: return(Expr.MultiplyAssign(left, right, method, conversion));

            case ExpressionType.MultiplyAssignChecked: return(Expr.MultiplyAssignChecked(left, right, method, conversion));

            case ExpressionType.MultiplyChecked: return(Expr.MultiplyChecked(left, right, method));

            case ExpressionType.NotEqual: return(Expr.NotEqual(left, right, liftToNull, method));

            case ExpressionType.Or: return(Expr.Or(left, right, method));

            case ExpressionType.OrAssign: return(Expr.OrAssign(left, right, method, conversion));

            case ExpressionType.OrElse: return(Expr.OrElse(left, right, method));

            case ExpressionType.Power: return(Expr.Power(left, right, method));

            case ExpressionType.PowerAssign: return(Expr.PowerAssign(left, right, method, conversion));

            case ExpressionType.RightShift: return(Expr.RightShift(left, right, method));

            case ExpressionType.RightShiftAssign: return(Expr.RightShiftAssign(left, right, method, conversion));

            case ExpressionType.Subtract: return(Expr.Subtract(left, right, method));

            case ExpressionType.SubtractAssign: return(Expr.SubtractAssign(left, right, method, conversion));

            case ExpressionType.SubtractAssignChecked: return(Expr.SubtractAssignChecked(left, right, method, conversion));

            case ExpressionType.SubtractChecked: return(Expr.SubtractChecked(left, right, method));

            default: throw new NotSupportedException();
            }
        }
Ejemplo n.º 3
0
        public static Expression CreateLambdaExpression <T, TModel>(IEnumerable <FilterElement> filterElements, Type querytype, ParameterExpression arg)
        {
            Expression expression = Expression.Constant(true);

            foreach (var filterElement in filterElements)
            {
                string[]   props = filterElement.FilterSpecs.DataElement.Split('.');
                Expression propertyExpression = arg;
                var        type = typeof(T);

                Expression notNullExpression = Expression.Constant(true);

                foreach (var property in  props)
                {
                    PropertyInfo pi = type.GetProperty(property);
                    propertyExpression = Expression.Property(propertyExpression, pi);
                    var nullExpression = Expression.Constant(GetNullExpressionForType(pi.PropertyType), pi.PropertyType);
                    notNullExpression = Expression.AndAlso(notNullExpression, Expression.NotEqual(propertyExpression, nullExpression));
                    type = pi.PropertyType;
                }

                var isLiteralType = type.IsEquivalentTo(typeof(string));

                var valueExpression = Expression.Constant(filterElement.FieldValue, filterElement.Property.PropertyType);

                Expression inputExpression, variableExpression;
                if (filterElement.FilterSpecs.CaseSensitive && isLiteralType)
                {
                    variableExpression = Expression.Call(propertyExpression,
                                                         typeof(String).GetMethod("ToUpper", new Type[] { }));

                    inputExpression = Expression.Call(valueExpression,
                                                      typeof(String).GetMethod("ToUpper", new Type[] { }));
                }
                else
                {
                    // special case to handle nullable values
                    if (valueExpression.Type.IsNullable())
                    {
                        valueExpression = Expression.Constant(valueExpression.Value, filterElement.Property.PropertyType.NullableOf());
                    }


                    inputExpression    = valueExpression;
                    variableExpression = propertyExpression;
                }

                BinaryExpression conditionExpression = Expression.Equal(variableExpression, valueExpression);

                switch (filterElement.FilterSpecs.OperatorOption)
                {
                case Operator.Equal:
                    conditionExpression = Expression.Equal(variableExpression, inputExpression);
                    break;

                case Operator.GreaterThan:
                    conditionExpression = Expression.GreaterThan(variableExpression, inputExpression);
                    break;

                case Operator.GreaterThanOrEqualTo:
                    conditionExpression = Expression.GreaterThanOrEqual(variableExpression, inputExpression);
                    break;

                case Operator.LessThan:
                    conditionExpression = Expression.LessThan(variableExpression, inputExpression);
                    break;

                case Operator.LessThanOrEqualTo:
                    conditionExpression = Expression.LessThanOrEqual(variableExpression, inputExpression);
                    break;

                case Operator.Unequal:
                    conditionExpression = Expression.NotEqual(variableExpression, inputExpression);
                    break;

                case Operator.Like:
                    MethodInfo method             = typeof(string).GetMethod("Contains", new[] { typeof(string) });
                    var        containsMethodCall = Expression.Call(variableExpression, method, inputExpression);
                    conditionExpression = Expression.Equal(containsMethodCall, Expression.Constant(true));
                    break;
                }


                var clausePart = Expression.AndAlso(notNullExpression, conditionExpression);

                if (!filterElement.FieldValue.Equals(GetNullExpressionForType(filterElement.Property.PropertyType)))
                {
                    expression = Expression.AndAlso(expression, clausePart);
                }
            }

            return(expression);
        }
Ejemplo n.º 4
0
 /// <summary>
 /// 创建小于等于运算表达式
 /// </summary>
 /// <param name="left">左操作数</param>
 /// <param name="right">右操作数</param>
 public static MicrosoftExpression LessEqual(this MicrosoftExpression left, MicrosoftExpression right)
 {
     return(MicrosoftExpression.LessThanOrEqual(left, right));
 }
Ejemplo n.º 5
0
            private static BinaryExpression LessThanOrEqualExpression(ParameterExpression parameter, string propertyName, object value)
            {
                var(bodyLeft, bodyRight) = GetBodyExpressions(parameter, propertyName, value);

                return(SystemExpression.LessThanOrEqual(bodyLeft, bodyRight));
            }
Ejemplo n.º 6
0
 public static BinaryExpression LessThanOrEqual(Expression left, Expression right, bool liftToNull, MethodInfo method) => Expression.LessThanOrEqual(left, right, liftToNull, method);
Ejemplo n.º 7
0
 public static BinaryExpression LessThanOrEqual(Expression left, Expression right) => Expression.LessThanOrEqual(left, right);
Ejemplo n.º 8
0
        private static Expression GetExpression <T>(ParameterExpression param, Filter filter)
        {
            object filterValue = filter.Value;
            Op     operation   = filter.Operation;

            MemberExpression   member = Expression.Property(param, filter.PropertyName);
            ConstantExpression constant;

            #region NULLABLE HANDLING
            if (filterValue != null && IsNullableType(member.Type))
            {
                var targetType    = Nullable.GetUnderlyingType(member.Type);
                var propertyValue = Convert.ChangeType(filterValue, targetType);
                constant = Expression.Constant(propertyValue, member.Type);
            }
            else
            {
                constant = Expression.Constant(filterValue);
            }
            #endregion

            switch (operation)
            {
            case Op.GreaterThan:
                return(Expression.GreaterThan(member, constant));

            case Op.GreaterThanOrEqual:
                return(Expression.GreaterThanOrEqual(member, constant));

            case Op.LessThan:
                return(Expression.LessThan(member, constant));

            case Op.LessThanOrEqual:
                return(Expression.LessThanOrEqual(member, constant));

            case Op.Equals:

                //string equals ignore case comparison
                if (member.Type.TypeHandle.Equals(typeof(string).TypeHandle))
                {
                    var compareExpression = Expression.Call(null, compareMethod, member, constant, Expression.Constant(StringComparison.InvariantCultureIgnoreCase));
                    return(Expression.Equal(compareExpression, Expression.Constant(0)));
                }

                return(Expression.Equal(member, constant));

            case Op.Contains:
                return(Expression.Call(member, containsMethod, constant));

            case Op.StartsWith:
                return(Expression.Call(member, startsWithMethod, constant));

            case Op.EndsWith:
                return(Expression.Call(member, endsWithMethod, constant));

            case Op.NotEqual:
                return(Expression.NotEqual(member, constant));

            case Op.HasFlag:
                constant = Expression.Constant(filter.Value, typeof(Enum));
                return(Expression.Call(member, hasFlagMethod, constant));
            }

            return(null);
        }
Ejemplo n.º 9
0
 public static Expression LessThanOrEqual(Expression arg0, Expression arg1)
 {
     return(new Expression(LinqExpression.LessThanOrEqual(arg0, arg1)));
 }