OrElse() public static method

Creates a BinaryExpression that represents a conditional OR operation that evaluates the second operand only if it has to.
public static OrElse ( 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
        /// <summary>
        /// Creates an expression that defines a logical OR function
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="left"></param>
        /// <param name="right"></param>
        /// <returns></returns>
        public static Expression <Func <T, bool> > Or <T>(this Expression <Func <T, bool> > left, Expression <Func <T, bool> > right)
        {
            var invokedExpr = XPR.Invoke(right, left.Parameters.Cast <XPR>());

            return(XPR.Lambda <Func <T, bool> >
                       (XPR.OrElse(left.Body, invokedExpr), left.Parameters));
        }
Ejemplo n.º 2
0
        public static Expression <Func <T, bool> > Or <T>(this Expression <Func <T, bool> > expr1,
                                                          Expression <Func <T, bool> > expr2)
        {
            var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast <Expression>());

            return(Expression.Lambda <Func <T, bool> >
                       (Expression.OrElse(expr1.Body, invokedExpr), expr1.Parameters));
        }
Ejemplo n.º 3
0
 private static void ConditionDivByOr(ref E condition, E target, MethodInfo mInfo, string value)
 {
     if (condition == null)
     {
         condition = (E)E.Call(target, mInfo, E.Constant(value));
     }
     else
     {
         condition = E.OrElse(condition, E.Call(target, mInfo, E.Constant(value)));
     }
 }
Ejemplo n.º 4
0
 /// <summary>
 /// 或操作表达式
 /// </summary>
 /// <param name="left">左操作数</param>
 /// <param name="right">右操作数</param>
 public static MicrosoftExpression Or(this MicrosoftExpression left, MicrosoftExpression right)
 {
     if (left == null)
     {
         return(right);
     }
     if (right == null)
     {
         return(left);
     }
     return(MicrosoftExpression.OrElse(left, right));
 }
Ejemplo n.º 5
0
        public void TestWhereAndOr()
        {
            var parameter = LinqExpression.Parameter(typeof(NumbersModel), "x");
            var n1        = LinqExpression.Property(parameter, "Number1");
            var n2        = LinqExpression.Property(parameter, "Number2");
            var cases     = new[] {
                Tuple.Create((LinqExpression)LinqExpression.AndAlso(LinqExpression.GreaterThan(n1, LinqExpression.Constant(3)), LinqExpression.GreaterThan(n2, LinqExpression.Constant(3))),
                             (Func <NumbersModel, object, bool>)TestWhereAndValidator, default(object), parameter),
                Tuple.Create((LinqExpression)LinqExpression.OrElse(LinqExpression.LessThan(n1, LinqExpression.Constant(3)), LinqExpression.LessThan(n2, LinqExpression.Constant(3))),
                             (Func <NumbersModel, object, bool>)TestWhereOrValidator, default(object), parameter)
            };

            LoadModelNumbers(10);
            RunTestWithNumbers(new[] { 3, 5 }, cases);
        }
Ejemplo n.º 6
0
            private static SystemExpression OrElseExpression(ParameterExpression parameter, IEnumerable <KendoFilter> filters)
            {
                SystemExpression body = null;

                foreach (var filter in filters)
                {
                    if (body == null)
                    {
                        body = ToExpression(parameter, filter);

                        continue;
                    }

                    body = SystemExpression.OrElse(body, ToExpression(parameter, filter));
                }

                return(body);
            }
        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.º 8
0
 public static Ex Or(this Ex me, Ex other) => Ex.OrElse(me, other);
Ejemplo n.º 9
0
 protected override BinaryExpression CreateBody(Expression left, Expression right)
 {
     return(Expression_.OrElse(left, right));
 }
Ejemplo n.º 10
0
        private static Exp CreateSubExpression(Exp pe, Type type, string text, FullTextSearchOptions options, bool isQueriable = false)
        {
            var texts = text.Split(new string[] { "||" }, StringSplitOptions.RemoveEmptyEntries)
                        .Select(t => t.Trim().ToLower())
                        .Where(t => t.Length > 0)
                        .ToList();

            var properties = type.GetProperties().AsEnumerable();

            if (isQueriable)
            {
                properties = properties.GetMappedProperties();
            }

            Exp predicateBody = null;

            foreach (var prop in properties)
            {
                //Check if we can use this property in Search
                if (options == null || options.Filter == null || options.Filter.Invoke(prop))
                {
                    var paramExp = Exp.Property(pe, prop);

                    if (prop.PropertyType == typeof(string) || prop.PropertyType == typeof(int) ||
                        prop.PropertyType == typeof(int?))
                    {
                        Exp notNullExp = null;
                        if (prop.PropertyType == typeof(string))
                        {
                            Exp nullConst = Exp.Constant(null, typeof(string));
                            notNullExp = Exp.NotEqual(paramExp, nullConst);
                        }

                        Exp toStringExp = paramExp;
                        //Check if property has int type
                        if (prop.PropertyType == typeof(int?) || prop.PropertyType == typeof(int))
                        {
                            if (!isQueriable)
                            {
                                Exp toNullableIntExp = null;

                                //property should be nullable
                                if (prop.PropertyType == typeof(int))
                                {
                                    toNullableIntExp = Exp.TypeAs(paramExp, typeof(int?));
                                }

                                Exp valueExp = null;
                                if (toNullableIntExp != null)
                                {
                                    notNullExp = Exp.Property(toNullableIntExp, "HasValue");
                                    valueExp   = Exp.Property(toNullableIntExp, "Value");
                                }
                                else
                                {
                                    notNullExp = Exp.Property(paramExp, "HasValue");
                                    valueExp   = Exp.Property(paramExp, "Value");
                                }

                                notNullExp = Exp.Equal(notNullExp, Exp.Constant(true, typeof(bool)));

                                var convertToString = typeof(Convert).GetMethod("ToString", new[] { typeof(int) });
                                toStringExp = Exp.Call(convertToString, valueExp);
                            }
                            else
                            {
                                var convertToString = typeof(Convert).GetMethod("ToString", new[] { typeof(int) });
                                toStringExp = Exp.Call(convertToString, Exp.Convert(paramExp, typeof(int)));
                            }
                        }

                        Exp peLower = Exp.Call(toStringExp, typeof(string).GetMethod("ToLower", System.Type.EmptyTypes));

                        Exp conditionExp = null;
                        foreach (var txt in texts)
                        {
                            var constant    = Exp.Constant(txt, typeof(string));
                            Exp containsExp = Exp.Call(peLower, typeof(string).GetMethod("Contains", new[] { typeof(string) }), constant);
                            if (conditionExp != null)
                            {
                                conditionExp = Exp.OrElse(conditionExp, containsExp);
                            }
                            else
                            {
                                conditionExp = containsExp;
                            }
                        }

                        Exp andExp = (notNullExp != null) ? Exp.AndAlso(notNullExp, conditionExp) : conditionExp;
                        if (predicateBody != null)
                        {
                            predicateBody = Exp.OrElse(predicateBody, andExp);
                        }
                        else
                        {
                            predicateBody = andExp;
                        }
                    }

                    //If this property is't simple and the depth > 0
                    if (options != null && options.Depth != 0 &&
                        !prop.PropertyType.IsSimpleType() && !prop.PropertyType.IsEnumerable())
                    {
                        options.Depth -= 1;
                        var subExp = CreateSubExpression(paramExp, prop.PropertyType, text, options, isQueriable);
                        options.Depth += 1;

                        if (subExp != null)
                        {
                            Exp notNullExp = Exp.NotEqual(paramExp, Exp.Constant(null, typeof(object)));
                            subExp        = Exp.AndAlso(notNullExp, subExp);
                            predicateBody = (predicateBody != null) ? Exp.OrElse(predicateBody, subExp) : subExp;
                        }
                    }
                }
            }

            return(predicateBody);
        }
Ejemplo n.º 11
0
        // Return value is used through reflection invocation
        // ReSharper disable once UnusedMethodReturnValue.Local
        private static Expression <Func <TClass, bool> > Convert <TProperty, TClass>(
            Expression <Func <TProperty, TProperty, bool> > expression,
            List <TProperty> constant,
            MemberExpression propertyExpression,
            ParameterExpression parameter)
        {
            // initialize the expression with a always false one to make chaining possible
            Expression curriedBody = null;

            foreach (TProperty c in constant)
            {
                // Initialize expression if this is the first loop, else chain the expression with an "orElse" Expression
                curriedBody = curriedBody == null ? new FilterLambdaVisitor <TProperty>(propertyExpression, c).Visit(expression.Body) : Expression.OrElse(curriedBody, new FilterLambdaVisitor <TProperty>(propertyExpression, c).Visit(expression.Body));
            }

            return(Expression.Lambda <Func <TClass, bool> >(curriedBody, parameter));
        }
Ejemplo n.º 12
0
 public static BinaryExpression or(XPR left, XPR right)
 => XPR.OrElse(left, right);