protected virtual IParsedLambda CreateParsedLambda()
        {
            //PERF: This ugly processing of nodes for both transformers in one loop is for performance. Perhaps move bach to older cleaner solution
            var nullableNodeTransformer = new NullableNodeTransformer();
            var boolNodeTransformer     = new BoolNodeTransformer();

            var newNodes = new NodesCollection();
            var maxIndex = Nodes.Count;

            for (var i = 0; i < Nodes.Count; i++)
            {
                var nullableNodes = nullableNodeTransformer.Transform(maxIndex, ref i, Nodes[i], Nodes);
                var merged        = nullableNodes.MergeWith(Nodes.Skip(i + 1)).ToArray();
                var maxIndexInner = nullableNodes.Length;
                for (var iInner = 0; iInner < maxIndexInner; iInner++)
                {
                    var boolNodes = boolNodeTransformer.Transform(maxIndexInner, ref iInner, merged[iInner], new NodesCollection(merged));
                    newNodes.AddNodes(boolNodes);
                }
            }

            return(new ParsedLambda(newNodes.ToArray()));
        }
Example #2
0
        public virtual IParsedLambda Parse(OrderByExpression[] orderByExpressions)
        {
            Ensure.That(orderByExpressions, "orderByExpressions").HasItems();

            var nodesContainer = new NodesCollection();

            foreach (var orderByExpression in orderByExpressions.Where(e => e != null))
            {
                var memberExpression = orderByExpression.InnerLambdaExpression.Body.GetRightMostMember();
                if (memberExpression == null)
                {
                    throw new SisoDbException(ExceptionMessages.OrderByExpressionDoesNotTargetMember.Inject(orderByExpression.ToString()));
                }

                var callExpression = (orderByExpression.InnerLambdaExpression.Body is UnaryExpression)
                    ? ((UnaryExpression)orderByExpression.InnerLambdaExpression.Body).Operand as MethodCallExpression
                                        : orderByExpression.InnerLambdaExpression.Body as MethodCallExpression;

                if (callExpression != null)
                {
                    throw new SisoDbException(ExceptionMessages.OrderByParser_UnsupportedMethodForSortingDirection);
                }

                var memberType = memberExpression.Type;
                if (memberType.IsEnumerableType())
                {
                    memberType = memberType.GetEnumerableElementType();
                }

                var sortDirection = orderByExpression is OrderByAscExpression ? SortDirections.Asc : SortDirections.Desc;
                var memberPath    = memberExpression.ToPath();
                var sortingNode   = new SortingNode(memberPath, memberType, DataTypeConverter.Convert(memberType, memberPath), sortDirection);
                nodesContainer.AddNode(sortingNode);
            }

            return(new ParsedLambda(nodesContainer.ToArray()));
        }