Exemple #1
0
        public override Expression Visit(Expression expression)
        {
            if (expression == null)
            {
                return(null);
            }

            if (!(expression is NewExpression ||
                  expression is MemberInitExpression ||
                  expression is EntityShaperExpression ||
                  expression is IncludeExpression))
            {
                // This skips the group parameter from GroupJoin
                if (expression is ParameterExpression parameter &&
                    parameter.Type.IsGenericType &&
                    parameter.Type.GetGenericTypeDefinition() == typeof(IEnumerable <>))
                {
                    return(parameter);
                }

                if (_clientEval)
                {
                    switch (expression)
                    {
                    case ConstantExpression _:
                        return(expression);

                    case ParameterExpression parameterExpression:
                        return(Expression.Call(
                                   _getParameterValueMethodInfo.MakeGenericMethod(parameterExpression.Type),
                                   QueryCompilationContext.QueryContextParameter,
                                   Expression.Constant(parameterExpression.Name)));

                    case MaterializeCollectionNavigationExpression materializeCollectionNavigationExpression:
                        return(_selectExpression.AddCollectionProjection(
                                   _queryableMethodTranslatingExpressionVisitor.TranslateSubquery(
                                       materializeCollectionNavigationExpression.Subquery),
                                   materializeCollectionNavigationExpression.Navigation, null));

                    case MethodCallExpression methodCallExpression:
                    {
                        if (methodCallExpression.Method.IsGenericMethod &&
                            methodCallExpression.Method.DeclaringType == typeof(Enumerable) &&
                            methodCallExpression.Method.Name == nameof(Enumerable.ToList))
                        {
                            var elementType = methodCallExpression.Method.GetGenericArguments()[0];

                            var result = _queryableMethodTranslatingExpressionVisitor.TranslateSubquery(methodCallExpression.Arguments[0]);

                            return(_selectExpression.AddCollectionProjection(result, null, elementType));
                        }

                        var subquery = _queryableMethodTranslatingExpressionVisitor.TranslateSubquery(methodCallExpression);

                        if (subquery != null)
                        {
                            if (subquery.ResultCardinality == ResultCardinality.Enumerable)
                            {
                                return(_selectExpression.AddCollectionProjection(subquery, null, subquery.ShaperExpression.Type));
                            }
        public override Expression Visit(Expression expression)
        {
            if (expression == null)
            {
                return(null);
            }

            if (!(expression is NewExpression ||
                  expression is MemberInitExpression ||
                  expression is EntityShaperExpression ||
                  expression is IncludeExpression))
            {
                // This skips the group parameter from GroupJoin
                if (expression is ParameterExpression parameter &&
                    parameter.Type.IsGenericType &&
                    parameter.Type.GetGenericTypeDefinition() == typeof(IEnumerable <>))
                {
                    return(parameter);
                }

                if (_clientEval)
                {
                    if (expression is ConstantExpression)
                    {
                        return(expression);
                    }

                    if (expression is ParameterExpression parameterExpression)
                    {
                        return(Expression.Call(
                                   _getParameterValueMethodInfo.MakeGenericMethod(parameterExpression.Type),
                                   QueryCompilationContext.QueryContextParameter,
                                   Expression.Constant(parameterExpression.Name)));
                    }

                    if (expression is MethodCallExpression methodCallExpression &&
                        methodCallExpression.Method.Name == "MaterializeCollectionNavigation")
                    {
                        var result     = _queryableMethodTranslatingExpressionVisitor.TranslateSubquery(methodCallExpression.Arguments[0]);
                        var navigation = (INavigation)((ConstantExpression)methodCallExpression.Arguments[1]).Value;

                        return(_selectExpression.AddCollectionProjection(result, navigation));
                    }

                    var translation = _sqlTranslator.Translate(expression);
                    if (translation == null)
                    {
                        return(base.Visit(expression));
                    }
                    else
                    {
                        return(new ProjectionBindingExpression(_selectExpression, _selectExpression.AddToProjection(translation), expression.Type));
                    }
                }
                else
                {
                    var translation = _sqlTranslator.Translate(expression);
                    if (translation == null)
                    {
                        return(null);
                    }

                    _projectionMapping[_projectionMembers.Peek()] = translation;

                    return(new ProjectionBindingExpression(_selectExpression, _projectionMembers.Peek(), expression.Type));
                }
            }

            return(base.Visit(expression));
        }
Exemple #3
0
        public override Expression?Visit(Expression?expression)
        {
            if (expression == null)
            {
                return(null);
            }

            if (!(expression is NewExpression ||
                  expression is MemberInitExpression ||
                  expression is EntityShaperExpression ||
                  expression is IncludeExpression))
            {
                // This skips the group parameter from GroupJoin
                if (expression is ParameterExpression parameter &&
                    parameter.Type.IsGenericType &&
                    parameter.Type.GetGenericTypeDefinition() == typeof(IEnumerable <>))
                {
                    return(parameter);
                }

                if (_clientEval)
                {
                    switch (expression)
                    {
                    case ConstantExpression _:
                        return(expression);

                    case ProjectionBindingExpression projectionBindingExpression:
                        if (projectionBindingExpression.Index is int index)
                        {
                            var newIndex = _selectExpression.AddToProjection(_existingProjections[index]);

                            return(new ProjectionBindingExpression(_selectExpression, newIndex, expression.Type));
                        }

                        if (projectionBindingExpression.ProjectionMember != null)
                        {
                            // This would be SqlExpression. EntityProjectionExpression would be wrapped inside EntityShaperExpression.
                            var mappedProjection = (SqlExpression)((SelectExpression)projectionBindingExpression.QueryExpression)
                                                   .GetMappedProjection(projectionBindingExpression.ProjectionMember);

                            return(new ProjectionBindingExpression(
                                       _selectExpression, _selectExpression.AddToProjection(mappedProjection), expression.Type));
                        }

                        throw new InvalidOperationException(CoreStrings.TranslationFailed(projectionBindingExpression.Print()));

                    case ParameterExpression parameterExpression:
                        if (parameterExpression.Name?.StartsWith(QueryCompilationContext.QueryParameterPrefix, StringComparison.Ordinal)
                            == true)
                        {
                            return(Expression.Call(
                                       _getParameterValueMethodInfo.MakeGenericMethod(parameterExpression.Type),
                                       QueryCompilationContext.QueryContextParameter,
                                       Expression.Constant(parameterExpression.Name)));
                        }

                        throw new InvalidOperationException(CoreStrings.TranslationFailed(parameterExpression.Print()));

                    case MaterializeCollectionNavigationExpression materializeCollectionNavigationExpression:
                        return(_selectExpression.AddCollectionProjection(
                                   _queryableMethodTranslatingExpressionVisitor.TranslateSubquery(
                                       materializeCollectionNavigationExpression.Subquery) !,
                                   materializeCollectionNavigationExpression.Navigation,
                                   materializeCollectionNavigationExpression.Navigation.ClrType.TryGetSequenceType() !));

                    case MethodCallExpression methodCallExpression:
                    {
                        if (methodCallExpression.Method.IsGenericMethod &&
                            methodCallExpression.Method.DeclaringType == typeof(Enumerable) &&
                            methodCallExpression.Method.Name == nameof(Enumerable.ToList))
                        {
                            var elementType = methodCallExpression.Method.GetGenericArguments()[0];

                            var result = _queryableMethodTranslatingExpressionVisitor.TranslateSubquery(
                                methodCallExpression.Arguments[0]);

                            if (result != null)
                            {
                                return(_selectExpression.AddCollectionProjection(result, null, elementType));
                            }
                        }
                        else
                        {
                            var subquery = _queryableMethodTranslatingExpressionVisitor.TranslateSubquery(methodCallExpression);
                            if (subquery != null)
                            {
                                if (subquery.ResultCardinality == ResultCardinality.Enumerable)
                                {
                                    return(_selectExpression.AddCollectionProjection(subquery, null, subquery.ShaperExpression.Type));
                                }