Exemplo n.º 1
0
        static CustomExpressionTree ProcessWhereStr(string whereStr)
        {
            var result        = new CustomExpressionTree();
            var junctionIndex = new List <Tuple <int, string> >();
            var index2        = 0;

            GetJunctionSort(whereStr, junctionIndex);
            var singleExps = whereStr.Split(junctionSplit, StringSplitOptions.RemoveEmptyEntries).ToList().Select(x => x.Replace(" ", "")).Where(x => x != "");

            foreach (var expression in singleExps)
            {
                var singleExp = expression.Split(OperSplit, StringSplitOptions.RemoveEmptyEntries).ToList().Select(x => x.Replace(" ", "")).ToArray();
                var subExp    = new CustomExpression {
                    ParameterName = singleExp[0], ParameterValue = singleExp[1]
                };
                var op = expression.Remove(expression.Length - singleExp[1].Length); //tail
                op        = op.Remove(0, singleExp[0].Length);                       // header
                subExp.Op = op;                                                      //operation in middle

                ProcessSubExp(subExp, result);
                if (index2 < junctionIndex.Count)
                {
                    var oper  = junctionIndex[index2];
                    var item2 = oper.Item2;
                    result.Push(item2);
                }
                index2++;
            }
            return(result);
        }
Exemplo n.º 2
0
        //StartWith Contains ...
        static IList <BinaryExpression> Lambda <TObj>(CustomExpressionTree expressTree, ParameterExpression pe) where TObj : class
        {
            var flagArr        = new List <int>();
            var expressionList = new List <BinaryExpression>();

            while (expressTree.Count > 0)
            {
                var stackContent = expressTree.Pop();
                if (stackContent is string)
                {
                    ProcessNonExp(flagArr, expressionList, stackContent.ToString());
                    continue;
                }

                if (stackContent is CustomExpression)
                {
                    var currExpress = (stackContent as CustomExpression);
                    ProcessExp <TObj>(pe, currExpress, flagArr, expressionList);
                }
            }
            while (flagArr.Count > 0)
            {
                var lastFlag = flagArr[flagArr.Count - 1];
                CompositeExpByConj(lastFlag, expressionList);
                flagArr.RemoveAt(flagArr.Count - 1);
            }
            return(expressionList);
        }
Exemplo n.º 3
0
        static CustomExpressionTree SwapExp(CustomExpressionTree result)
        {
            var result2 = new CustomExpressionTree();

            while (result.Count > 0)
            {
                result2.Push(result.Pop());
            }
            return(result2);
        }
Exemplo n.º 4
0
        static Func <TObj, bool> Convert <TObj>(string whereStr) where TObj : class
        {
            var result = new CustomExpressionTree();

            result = ProcessWhereStr(whereStr);
            var result2 = SwapExp(result);

            var pe             = Expression.Parameter(typeof(TObj), "x");
            var bianryExprestt = Lambda <TObj>(result2, pe).First();

            return(Expression.Lambda <Func <TObj, bool> >(bianryExprestt, new[] { pe }).Compile());
        }
Exemplo n.º 5
0
        static void ProcessSubExp(CustomExpression subExp, CustomExpressionTree result)
        {
            var index = 0;

            do
            {
                index = subExp.ParameterName.IndexOf(ConstBrace.LeftBrace);
                if (index < 0)
                {
                    break;
                }
                result.Push(ConstBrace.LeftBrace);
                subExp.ParameterName = subExp.ParameterName.Remove(index, 1);
            } while (index >= 0);

            var flagEnd       = false;
            var cntRightBrace = 0;

            index = 0;
            do
            {
                index = subExp.ParameterValue.IndexOf(ConstBrace.RightBrace);
                if (index < 0)
                {
                    break;
                }
                cntRightBrace++;
                subExp.ParameterValue = subExp.ParameterValue.Remove(index, 1);
                flagEnd = true;
            } while (index >= 0);

            result.Push(subExp);

            if (flagEnd)
            {
                while (cntRightBrace-- > 0)
                {
                    result.Push(ConstBrace.RightBrace);
                }
            }
        }