Пример #1
0
        protected override ShapedQueryExpression TranslateTake(ShapedQueryExpression source, Expression count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (count == null)
            {
                throw new ArgumentNullException(nameof(count));
            }

            var selectExpression = (SelectExpression)source.QueryExpression;
            var translation      = _sqlTranslator.Translate(count);

            if (translation != null)
            {
                selectExpression.ApplyLimit(translation);

                return(source);
            }

            return(null);
        }
        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));
                            }
                        }

                        break;
                    }
                    }

                    var translation = _sqlTranslator.Translate(expression);
                    return(translation == null
                        ? base.Visit(expression)
                        : 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));
        }