示例#1
0
        public void AndOrOperator_Or_ReturnResult()
        {
            /*
             * 0011 0010 = 50
             * 0000 1011 = 11
             * ---------
             * 0011 1011
             */

            int    number1        = 11;
            int    number2        = 50;
            string numberExpected = "59";
            string binaryExpected = "111011";

            Result actual = AndOrOperator.SolutionOr(number1, number2);

            Assert.True(actual.Number == numberExpected && actual.Binary == binaryExpected);
        }
示例#2
0
        public static Expression <Func <T, bool> > AsPredicate <T>(this string filterString, AndOrOperator andOrOperator = AndOrOperator.Or) where T : IEntityBase
        {
            var predicate  = PredicateBuilder.New <T>();
            var param      = Expression.Parameter(typeof(T), "x");
            var list       = new List <Expression>();
            var queryparts = filterString.Split('|');

            //
            //   MethodInfo StringContainsMethod = typeof(string).GetMethod(@"Contains");

            foreach (var part in queryparts)
            {
                var elements = part.Trim().Split(' ');
                //
                var propertie = elements[0];
                var value     = elements[2];
                #region PendingForRemove
                //var propertyAccessExpr = Expression.PropertyOrField(param, propertie);
                //Expression valueExpr;

                //try
                //{
                //    valueExpr = ToExprConstant(propertyAccessExpr.Type, value);
                //}
                //catch
                //{
                //    continue;

                //}

                //if (propertyAccessExpr.Type == typeof(string) ||)
                //{
                //    var currentMethodInfo = StringContainsMethod;
                //    var body = Expression.Call(propertyAccessExpr, currentMethodInfo, valueExpr);
                //    _list.Add(body);
                //}
                //else if (propertyAccessExpr.Type == typeof(Guid) || propertyAccessExpr.Type == typeof(bool))
                //{
                //    var expr = Expression.Equal(propertyAccessExpr, valueExpr);
                //    _list.Add(expr);
                //}
                //else
                //{
                //var expreTest = Test.ToExpression2<T>(param, propertie, elements[1], value);
                //if (expreTest != null)
                //    _list.Add(expreTest);
                //}

                #endregion

                var expreTest = Test.ToExpression2 <T>(param, propertie, elements[1], value);
                if (expreTest != null)
                {
                    list.Add(expreTest);
                }
            }

            if (list.Count <= 0)
            {
                return(predicate);
            }
            var tmp = list.Aggregate((expression, expression1) => andOrOperator == AndOrOperator.Or
                ? Expression.Or(expression, expression1)
                : Expression.And(expression, expression1));
            predicate.And(Expression.Lambda <Func <T, bool> >(tmp, param));

            return(predicate);
        }