Example #1
0
 private void AddLogicalOperator(AdoQueryPayload queryPayload, DslLogicalOperator logicalOperator)
 {
     if (logicalOperator == DslLogicalOperator.And)
     {
         queryPayload.Append("AND ");
     }
     else if (logicalOperator == DslLogicalOperator.Or)
     {
         queryPayload.Append("OR ");
     }
 }
        public static Expression <Func <T, bool> > BuildExpression <T>(this IEnumerable <T> source, IList <MatchCondition> conditions, ParameterExpression pe = null)
        {
            Expression <Func <T, bool> > returnCondition = null;
            Expression         cond1           = null;
            Expression         currentCond     = null;
            DslLogicalOperator currentOperator = DslLogicalOperator.NotDefined;

            if (pe == null)
            {
                pe = Expression.Parameter(typeof(T), "s");
            }



            foreach (var x in conditions)
            {
                switch (x.Operator)
                {
                case DslOperator.Like:
                    currentCond = Expression.Call(typeof(LinqExtension).GetMethod("RegexMatch", new Type[] { typeof(string), typeof(string) }), new Expression[] {
                        GetExpressionFromIValue(pe, x.Value1, typeof(string)),
                        GetExpressionFromIValue(pe, x.Value2, typeof(string))
                    });
                    break;

                case DslOperator.Is:
                    if (!(x.Value1 is VariableValue))
                    {
                        throw new ArgumentException("Left Side Value of IS must be a Variable");
                    }
                    if ((x.Value2 as NullValue).value != null)
                    {
                        throw new ArgumentException("IS Operator only Supports NULL");
                    }
                    currentCond = Expression.Equal(Expression.Convert(GetExpressionFromIValue(pe, x.Value1, GetTargetType(x, typeof(T))), typeof(object)), Expression.Constant(null));
                    break;

                case DslOperator.In:
                    currentCond = Expression.Call(typeof(LinqExtension).GetMethod("InMethod"), new Expression[] {
                        GetExpressionFromIValue(pe, x.Value1, typeof(string)),
                        GetExpressionFromIValue(pe, x.Value2, typeof(IList <string>))
                    });
                    break;

                case DslOperator.NotEquals:
                    currentCond = Expression.NotEqual(GetExpressionFromIValue(pe, x.Value1, GetTargetType(x, typeof(T))), GetExpressionFromIValue(pe, x.Value2, GetTargetType(x, typeof(T))));
                    //currentCond = Expression.NotEqual(Expression.Property(pe, x.VariableName), Expression.Constant(GetStringAsObject(x.Value, GetPropertType(x.VariableName, typeof(T)))));
                    break;

                case DslOperator.Equals:
                    currentCond = Expression.Equal(GetExpressionFromIValue(pe, x.Value1, GetTargetType(x, typeof(T))), GetExpressionFromIValue(pe, x.Value2, GetTargetType(x, typeof(T))));
                    //currentCond = Expression.Equal(Expression.Property(pe, x.VariableName), Expression.Constant(GetStringAsObject(x.Value, GetPropertType(x.VariableName, typeof(T)))));
                    break;

                case DslOperator.Greater:
                    currentCond = Expression.GreaterThan(GetExpressionFromIValue(pe, x.Value1, GetTargetType(x, typeof(T))), GetExpressionFromIValue(pe, x.Value2, GetTargetType(x, typeof(T))));
                    //currentCond = Expression.GreaterThan(Expression.Property(pe, x.VariableName), Expression.Constant(GetStringAsObject(x.Value, GetPropertType(x.VariableName, typeof(T)))));
                    break;

                case DslOperator.GreaterOrEqual:
                    currentCond = Expression.GreaterThanOrEqual(GetExpressionFromIValue(pe, x.Value1, GetTargetType(x, typeof(T))), GetExpressionFromIValue(pe, x.Value2, GetTargetType(x, typeof(T))));
                    //currentCond = Expression.GreaterThanOrEqual(Expression.Property(pe, x.VariableName), Expression.Constant(GetStringAsObject(x.Value, GetPropertType(x.VariableName, typeof(T)))));
                    break;

                case DslOperator.Lower:
                    currentCond = Expression.LessThan(GetExpressionFromIValue(pe, x.Value1, GetTargetType(x, typeof(T))), GetExpressionFromIValue(pe, x.Value2, GetTargetType(x, typeof(T))));
                    //currentCond = Expression.LessThan(Expression.Property(pe, x.VariableName), Expression.Constant(GetStringAsObject(x.Value, GetPropertType(x.VariableName, typeof(T)))));
                    break;

                case DslOperator.LowerOrEqual:
                    currentCond = Expression.LessThanOrEqual(GetExpressionFromIValue(pe, x.Value1, GetTargetType(x, typeof(T))), GetExpressionFromIValue(pe, x.Value2, GetTargetType(x, typeof(T))));
                    //currentCond = Expression.LessThanOrEqual(Expression.Property(pe, x.VariableName), Expression.Constant(GetStringAsObject(x.Value, GetPropertType(x.VariableName, typeof(T)))));
                    break;

                case DslOperator.OpenParenthesis:
                    currentCond = BuildExpression(source, x.SubConditions, pe).Body;
                    break;
                }
                if (x.Not)
                {
                    currentCond = Expression.Not(currentCond);
                }
                if (currentOperator == DslLogicalOperator.And)
                {
                    currentCond = Expression.AndAlso(cond1, currentCond);
                }
                else if (currentOperator == DslLogicalOperator.Or)
                {
                    currentCond = Expression.OrElse(cond1, currentCond);
                }

                cond1           = currentCond;
                currentOperator = x.NextLogOperator;
            }



            returnCondition = Expression.Lambda <Func <T, bool> >(currentCond, new[] { pe });
            return(returnCondition);
        }