public override Expression Translate(ExpandedNavigationSelectItem item)
        {
            var        segment            = (NavigationPropertySegment)item.PathToNavigationProperty.LastSegment;
            Expression expression         = Translate(segment);
            Type       navigationItemType = expression.Type;

            Type itemType = OeExpressionHelper.GetCollectionItemType(navigationItemType);

            if (itemType != null)
            {
                var expressionBuilder = new OeExpressionBuilder(_model, itemType);
                expression = expressionBuilder.ApplyFilter(expression, item.FilterOption);
                expression = expressionBuilder.ApplyOrderBy(expression, item.OrderByOption);

                var path = new ODataPath(_path.Union(item.PathToNavigationProperty));
                expression = expressionBuilder.ApplySkip(expression, item.SkipOption, path);
                expression = expressionBuilder.ApplyTake(expression, item.TopOption, path);

                foreach (KeyValuePair <ConstantExpression, ConstantNode> constant in expressionBuilder.Constants)
                {
                    _visitor.AddConstant(constant.Key, constant.Value);
                }
            }

            Expression countExpression = null;

            if (item.CountOption.GetValueOrDefault())
            {
                MethodInfo countMethodInfo = OeMethodInfoHelper.GetCountMethodInfo(itemType);
                countExpression = Expression.Call(countMethodInfo, expression);
            }

            if (item.SelectAndExpand.SelectedItems.Any())
            {
                var        path             = new ODataPath(_path.Union(item.PathToNavigationProperty));
                var        selectTranslator = new OeSelectTranslator(_visitor, path);
                Expression nestedExpression = selectTranslator.CreateExpression(expression, item.SelectAndExpand, OeMetadataLevel.Minimal);

                Type nestedType = OeExpressionHelper.GetCollectionItemType(nestedExpression.Type);
                if (nestedType == null)
                {
                    var visitor = new ParameterVisitor(selectTranslator._parameter, expression);
                    nestedExpression = visitor.Visit(nestedExpression);
                    nestedType       = nestedExpression.Type;
                }

                _selectItemInfo.EntryFactory = selectTranslator.CreateNestedEntryFactory(nestedType, _selectItemInfo.EntitySet, _selectItemInfo.ResourceInfo);
                expression = nestedExpression;
            }

            if (countExpression != null)
            {
                return(CreateNavigationLinkInfo(navigationItemType, expression, countExpression));
            }

            return(expression);
        }
示例#2
0
        public Expression ApplySelect(Expression source, SelectExpandClause selectClause, ODataPath path, OeMetadataLevel metadatLevel)
        {
            if (selectClause == null)
            {
                return(source);
            }

            var        selectTranslator = new OeSelectTranslator(_visitor, path);
            Expression selectExpression = selectTranslator.Build(source, selectClause, metadatLevel);

            _entryFactory = selectTranslator.CreateEntryFactory;

            Type selectItemType = OeExpressionHelper.GetCollectionItemType(selectExpression.Type);

            _visitor = new OeQueryNodeVisitor(_model, Expression.Parameter(selectItemType), _visitor.Constans);

            return(selectExpression);
        }
        public Expression ApplySelect(Expression source, OeQueryContext queryContext)
        {
            if (queryContext.ODataUri.SelectAndExpand == null && (queryContext.ODataUri.OrderBy == null || queryContext.PageSize == 0))
            {
                return(source);
            }

            var        selectTranslator = new OeSelectTranslator(_visitor, queryContext.ODataUri.Path);
            Expression selectExpression = selectTranslator.Build(source, queryContext);

            if (selectExpression == null)
            {
                return(source);
            }

            _entryFactory = selectTranslator.CreateEntryFactory;

            Type selectItemType = OeExpressionHelper.GetCollectionItemType(selectExpression.Type);

            _visitor = new OeQueryNodeVisitor(_model, Expression.Parameter(selectItemType), _visitor.Constans);

            return(selectExpression);
        }
            public override SelectItemInfo Translate(ExpandedNavigationSelectItem item)
            {
                var segment = (NavigationPropertySegment)item.PathToNavigationProperty.LastSegment;

                if (_navigationNextLink && segment.NavigationProperty.Type is IEdmCollectionTypeReference)
                {
                    return(null);
                }

                SelectItemInfo selectItemInfo        = CreateNavigationSelectItemInfo(_model, segment, false, item.CountOption);
                PropertyInfo   navigationClrProperty = _parameter.Type.GetProperty(selectItemInfo.EdmProperty.Name);
                Expression     expression            = Expression.MakeMemberAccess(_parameter, navigationClrProperty);

                Type itemType = OeExpressionHelper.GetCollectionItemType(expression.Type);

                if (itemType != null)
                {
                    var expressionBuilder = new OeExpressionBuilder(_model, itemType);
                    expression = expressionBuilder.ApplyFilter(expression, item.FilterOption);
                    expression = expressionBuilder.ApplyOrderBy(expression, item.OrderByOption);

                    var path = new ODataPath(_path.Union(item.PathToNavigationProperty));
                    expression = expressionBuilder.ApplySkip(expression, item.SkipOption, path);
                    expression = expressionBuilder.ApplyTake(expression, item.TopOption, path);

                    foreach (KeyValuePair <ConstantExpression, ConstantNode> constant in expressionBuilder.Constants)
                    {
                        _visitor.AddConstant(constant.Key, constant.Value);
                    }
                }

                if (item.SelectAndExpand.SelectedItems.Any())
                {
                    var path             = new ODataPath(_path.Union(item.PathToNavigationProperty));
                    var selectTranslator = new OeSelectTranslator(_visitor, path);

                    ParameterExpression parameter = Expression.Parameter(itemType ?? expression.Type);
                    selectTranslator.BuildSelect(item.SelectAndExpand, expression, parameter, _metadataLevel, _navigationNextLink);

                    Expression nestedExpression;
                    Type       nestedType;
                    if (itemType == null)
                    {
                        nestedExpression = selectTranslator.CreateTupleExpression();
                        var visitor = new ParameterVisitor(parameter, expression, segment.NavigationProperty.Type.IsNullable);
                        nestedExpression = visitor.Visit(nestedExpression);
                        nestedType       = nestedExpression.Type;
                    }
                    else
                    {
                        nestedExpression = selectTranslator.CreateSelectExpression(expression, parameter);
                        nestedType       = OeExpressionHelper.GetCollectionItemType(nestedExpression.Type);
                    }

                    selectItemInfo.EntryFactory = selectTranslator.CreateNestedEntryFactory(nestedType, selectItemInfo.EntitySet, selectItemInfo.Resource);
                    expression = nestedExpression;
                }

                selectItemInfo.Expression = expression;
                return(selectItemInfo);
            }