Ejemplo n.º 1
0
            private bool CanTranslate(Expression expression)
            {
                // Check for Query shape
                if (IsAggregateGroupBySelector(expression))
                {
                    var groupByResultOperator =
                        (GroupResultOperator)((SubQueryExpression)((MainFromClause)_groupQuerySource).FromExpression)
                        .QueryModel.ResultOperators
                        .Last();

                    _keySelector = groupByResultOperator.KeySelector;
                    var elementSelector = groupByResultOperator.ElementSelector;

                    if (!(elementSelector is QuerySourceReferenceExpression) &&
                        _sqlTranslatingExpressionVisitorFactory.Create(
                            _queryModelVisitor,
                            _selectExpression)
                        .Visit(groupByResultOperator.ElementSelector) == null)
                    {
                        return(false);
                    }

                    _selectExpression.ClearOrderBy();
                    _selectExpression.ClearProjection();

                    UpdateGroupQuerySourceParameter(typeof(ValueBuffer));

                    _projectionExpressionVisitor.Visit(_keySelector);
                    var columns = _selectExpression.Projection.ToArray();
                    _selectExpression.ClearProjection();

                    if (!(elementSelector is QuerySourceReferenceExpression))
                    {
                        _projectionExpressionVisitor.Visit(groupByResultOperator.ElementSelector);
                    }

                    if (_selectExpression.Projection.Count > 1)
                    {
                        _selectExpression.ClearProjection();
                    }

                    _selectExpression.AddToGroupBy(columns);

                    var shapedQuery =
                        (MethodCallExpression)((MethodCallExpression)_queryModelVisitor.Expression).Arguments[0];

                    var valueBufferShaper = new ValueBufferShaper(_groupQuerySource);

                    var groupShapedQuery = Expression.Call(
                        _queryModelVisitor.QueryCompilationContext
                        .QueryMethodProvider
                        .ShapedQueryMethod
                        .MakeGenericMethod(valueBufferShaper.Type),
                        shapedQuery.Arguments[0],
                        shapedQuery.Arguments[1],
                        Expression.Constant(valueBufferShaper));

                    _queryModelVisitor.Expression = groupShapedQuery;

                    UpdateGroupQuerySourceParameter(groupShapedQuery.Type);

                    return(true);
                }

                return(false);
            }