Exemple #1
0
        public void LogicalStandaloneExpressionTest()
        {
            Dictionary <string, object> valueStack = new Dictionary <string, object>();

            valueStack.Add("str", "test");
            valueStack.Add("int", 5);
            valueStack.Add("date", new DateTime(1989, 11, 14));

            ExpressionFactory factory = new ExpressionFactory(valueStack);

            factory.AddParameter("date", new DateTime(2012, 1, 1));
            factory.AddParameter("null", null);

            bool expected = (string)factory.ValueStack["str"] == "test" &&
                            ((int)factory.ValueStack["int"] >= 5 || (DateTime)factory.ValueStack["date"] != new DateTime(1989, 11, 14) &&
                             ((DateTime)factory.Parameters["date"]).AddYears(1).Year < 2000) &&
                            ((string)factory.ValueStack["str"]).Contains("na");

            var func = factory.BuildLogicalExpression(@"
$str == 'test' 
&& (  $int >= 5 || $date != 1989.11.14) 
&& @date.AddYears(1).Year > 2000
&& $str.Contains('na')
&& @null == NuLL");

            bool actual = func(null);

            Assert.AreEqual(expected, actual);
        }
Exemple #2
0
        public ProxyCollection <T> Where(string predicate, IEnumerable conditionParameters)
        {
            Dictionary <string, object> parameters = new Dictionary <string, object>();
            int i = 1;

            foreach (var parameter in conditionParameters)
            {
                object p = parameter;

                // it is need for calculating linq
                IEnumerable enumerable = parameter as IEnumerable;
                if (enumerable != null && !(parameter is string))
                {
                    ArrayList list = new ArrayList();
                    foreach (var item in enumerable)
                    {
                        list.Add(item);
                    }
                    p = list;
                }

                parameters.Add(string.Format("p{0}", i), p);
                i++;
            }

            ExpressionFactory   factory = new ExpressionFactory(_context, typeof(T), parameters);
            Func <object, bool> func    = factory.BuildLogicalExpression(predicate);

            IEnumerable <T> c = _collection.Where <T>(val => func(val));

            return(new ProxyCollection <T>(c, _context));
        }
Exemple #3
0
        public bool BooleanExpression(String expression, object root = null)
        {
            if (expression.StartsWith("$") && expression.Contains("("))
            {
                return((bool)CallScript(expression));
            }
            if (expression.StartsWith("@"))
            {
                return((bool)CallVariable(expression));
            }

            ExpressionFactory factory = new ExpressionFactory(this.Values);

            Func <object, bool> func = factory.BuildLogicalExpression(expression);

            return(func(null));
        }