Example #1
0
        /// <summary>
        ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
        ///     directly from your code. This API may change or be removed in future releases.
        /// </summary>
        protected override Expression VisitConstant(ConstantExpression constantExpression)
        {
            if (constantExpression.IsEntityQueryable())
            {
                var type       = ((IQueryable)constantExpression.Value).ElementType;
                var entityType = _queryCompilationContext.Model.FindEntityType(type)?.RootType();

                if (entityType != null)
                {
                    Expression newExpression = constantExpression;

                    if (entityType.FindPrimaryKey() == null)
                    {
                        IsKeylessQuery = true;

                        var query = entityType.GetDefiningQuery();

                        if (query != null &&
                            _entityQueryModelVisitor.ShouldApplyDefiningQuery(entityType, _querySource))
                        {
                            var parameterizedQuery
                                = _parameterExtractingExpressionVisitor.ExtractParameters(query.Body);

                            var subQueryModel = _queryModelGenerator.ParseQuery(parameterizedQuery);

                            newExpression = new SubQueryExpression(subQueryModel);
                        }
                    }

                    if (!_queryCompilationContext.IgnoreQueryFilters &&
                        entityType.GetQueryFilter() != null)
                    {
                        var parameterizedFilter
                            = (LambdaExpression)_parameterExtractingExpressionVisitor
                              .ExtractParameters(entityType.GetQueryFilter());

                        var oldParameterExpression = parameterizedFilter.Parameters[0];
                        var newParameterExpression = Expression.Parameter(type, oldParameterExpression.Name);

                        var predicateExpression
                            = ReplacingExpressionVisitor
                              .Replace(
                                  oldParameterExpression,
                                  newParameterExpression,
                                  parameterizedFilter.Body);

                        var whereExpression
                            = Expression.Call(
                                  _whereMethod.MakeGenericMethod(type),
                                  newExpression,
                                  Expression.Lambda(
                                      predicateExpression,
                                      newParameterExpression));

                        var subQueryModel = _queryModelGenerator.ParseQuery(whereExpression);

                        newExpression = new SubQueryExpression(subQueryModel);
                    }

                    return(newExpression);
                }
            }

            return(constantExpression);
        }
        /// <summary>
        ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
        ///     directly from your code. This API may change or be removed in future releases.
        /// </summary>
        protected override Expression VisitConstant(ConstantExpression constantExpression)
        {
            if (constantExpression.IsEntityQueryable())
            {
                var parameterizableQueryable = (IParameterizableQueryable)constantExpression.Value;
                var type       = ((IQueryable)constantExpression.Value).ElementType;
                var entityType = _queryCompilationContext.Model.FindEntityType(type)?.RootType();

                if (entityType != null)
                {
                    Expression newExpression = constantExpression;

                    if (entityType.IsQueryType)
                    {
                        IsViewTypeQuery = true;

                        var query = entityType.DefiningQuery;

                        if (query != null &&
                            _entityQueryModelVisitor.ShouldApplyDefiningQuery(entityType, _querySource) &&
                            parameterizableQueryable.DefiningParameterizedQuery == null)
                        {
                            var parameterizedDefiningQuery
                                = _queryModelGenerator
                                  .ExtractParameters(
                                      _queryCompilationContext.Logger,
                                      query.Body,
                                      _parameters,
                                      parameterize: false,
                                      generateContextAccessors: true);

                            var subQueryModel = _queryModelGenerator.ParseQuery(parameterizedDefiningQuery);

                            newExpression = new SubQueryExpression(subQueryModel);
                        }
                    }

                    if (parameterizableQueryable.DefiningParameterizedQuery != null &&
                        parameterizableQueryable.DefiningParameter != null)
                    {
                        var parameterizedQuery = parameterizableQueryable.DefiningParameterizedQuery.Query;

                        var parsedParameterizedQuery
                            = _queryModelGenerator
                              .ExtractParameters(
                                  _queryCompilationContext.Logger,
                                  parameterizedQuery.Body,
                                  _parameters,
                                  parameterize: false,
                                  generateContextAccessors: true);

                        var scopedParameterizedQuery = new ParameterScopeExpression(parsedParameterizedQuery);

                        var subQueryModel = _queryModelGenerator.ParseQuery(scopedParameterizedQuery);

                        newExpression = new SubQueryExpression(subQueryModel);
                    }

                    if (!_queryCompilationContext.IgnoreQueryFilters &&
                        entityType.QueryFilter != null)
                    {
                        var parameterizedFilter
                            = (LambdaExpression)_queryModelGenerator
                              .ExtractParameters(
                                  _queryCompilationContext.Logger,
                                  entityType.QueryFilter,
                                  _parameters,
                                  parameterize: false,
                                  generateContextAccessors: true);

                        var oldParameterExpression = parameterizedFilter.Parameters[0];
                        var newParameterExpression = Expression.Parameter(type, oldParameterExpression.Name);

                        var predicateExpression
                            = ReplacingExpressionVisitor
                              .Replace(
                                  oldParameterExpression,
                                  newParameterExpression,
                                  parameterizedFilter.Body);

                        var whereExpression
                            = Expression.Call(
                                  _whereMethod.MakeGenericMethod(type),
                                  newExpression,
                                  Expression.Lambda(
                                      predicateExpression,
                                      newParameterExpression));

                        var subQueryModel = _queryModelGenerator.ParseQuery(whereExpression);

                        newExpression = new SubQueryExpression(subQueryModel);
                    }

                    return(newExpression);
                }
            }

            return(constantExpression);
        }