Skip to content

Query: compilation error for queries projecting optional navigation, paging and another projecting of optional navigation #6618

Closed

Description

@maumar

Repro using ComplexQueryModel:

                var query = context.LevelOne
                    .Select(l1 => l1.OneToOne_Optional_FK)
                    .OrderBy(l2 => l2.Id)
                    .Take(10)
                    .Select(l2 => l2.OneToOne_Optional_FK.Name);

Exception:

variable 'l1.OneToOne_Optional_FK' of type 'Microsoft.EntityFrameworkCore.Specification.Tests.TestModels.ComplexNavigationsModel.Level2' referenced from scope '', but it is not defined
at System.Linq.Expressions.Compiler.VariableBinder.Reference(ParameterExpression node, VariableStorageKind storage)
at System.Linq.Expressions.Compiler.VariableBinder.VisitParameter(ParameterExpression node)
at System.Linq.Expressions.ParameterExpression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at System.Linq.Expressions.ExpressionVisitor.Visit(ReadOnlyCollection1 nodes) at System.Linq.Expressions.Compiler.VariableBinder.VisitLambda[T](Expression1 node)
at System.Linq.Expressions.Expression1.Accept(ExpressionVisitor visitor) at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node) at System.Linq.Expressions.ExpressionVisitor.VisitArguments(IArgumentProvider nodes) at System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression node) at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor) at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node) at System.Linq.Expressions.ExpressionVisitor.VisitArguments(IArgumentProvider nodes) at System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression node) at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor) at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node) at System.Linq.Expressions.ExpressionVisitor.VisitArguments(IArgumentProvider nodes) at System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression node) at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor) at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node) at System.Linq.Expressions.ExpressionVisitor.VisitArguments(IArgumentProvider nodes) at System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression node) at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor) at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node) at System.Linq.Expressions.ExpressionVisitor.VisitArguments(IArgumentProvider nodes) at System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression node) at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor) at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node) at System.Linq.Expressions.ExpressionVisitor.Visit(ReadOnlyCollection1 nodes)
at System.Linq.Expressions.Compiler.VariableBinder.VisitLambda[T](Expression1 node) at System.Linq.Expressions.Expression1.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at System.Linq.Expressions.Compiler.LambdaCompiler.Compile(LambdaExpression lambda, DebugInfoGenerator debugInfoGenerator)
at System.Linq.Expressions.Expression1.Compile() Query\EntityQueryModelVisitor.cs(659,0): at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CreateExecutorLambda[TResults]() Query\EntityQueryModelVisitor.cs(248,0): at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CreateQueryExecutor[TResult](QueryModel queryModel) Storage\Database.cs(67,0): at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](QueryModel queryModel) --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() Query\Internal\QueryCompiler.cs(213,0): at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass19_01.b__0()
Query\Internal\CompiledQueryCache.cs(49,0): at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func1 compiler) Query\Internal\QueryCompiler.cs(168,0): at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQuery[TResult](Expression query) Query\Internal\QueryCompiler.cs(98,0): at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query) Query\Internal\EntityQueryProvider.cs(57,0): at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression) at Remotion.Linq.QueryableBase1.GetEnumerator()
at System.Collections.Generic.List1..ctor(IEnumerable1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)

Activity

  1. added this to the 1.1.0 milestone on Oct 3, 2016
  2. modified the milestones: , 1.1.0 on Oct 5, 2016
  3. smitpatel commented on Nov 30, 2016

    @smitpatel
    Contributor

    Exception message changed here

     Microsoft.EntityFrameworkCore.SqlServer.FunctionalTests.ComplexNavigationsQuerySqlServerTest.Optional_navigation_take_optional_navigation [FAIL]
          System.InvalidCastException : Unable to cast object of type 'System.Linq.Expressions.UnaryExpression' to type 'Microsoft.EntityFrameworkCore.Query.Expressions.AliasExpression'.
          Stack Trace:
               at System.Linq.Enumerable.<CastIterator>d__94`1.MoveNext()
            src\Microsoft.EntityFrameworkCore.Relational\Query\Expressions\SelectExpression.cs(937,0): at Microsoft.EntityFrameworkCore.Query.Expressions.SelectExpression.ExplodeStarProjection()
            src\Microsoft.EntityFrameworkCore.Relational\Query\RelationalQueryModelVisitor.cs(1076,0): at Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.QuerySourceUpdater.VisitConstant(ConstantExpression constantExpression)
            src\Microsoft.EntityFrameworkCore\Query\ExpressionVisitors\ExpressionVisitorBase.cs(26,0): at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.ExpressionVisitorBase.Visit(Expression node)
               at System.Linq.Expressions.ExpressionVisitor.VisitAndConvert[T](ReadOnlyCollection`1 nodes, String callerName)
            src\Microsoft.EntityFrameworkCore.Relational\Query\RelationalQueryModelVisitor.cs(1084,0): at Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.QuerySourceUpdater.VisitMethodCall(MethodCallExpression methodCallExpression)
            src\Microsoft.EntityFrameworkCore\Query\ExpressionVisitors\ExpressionVisitorBase.cs(26,0): at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.ExpressionVisitorBase.Visit(Expression node)
               at System.Linq.Expressions.ExpressionVisitor.VisitAndConvert[T](ReadOnlyCollection`1 nodes, String callerName)
            src\Microsoft.EntityFrameworkCore.Relational\Query\RelationalQueryModelVisitor.cs(1084,0): at Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.QuerySourceUpdater.VisitMethodCall(MethodCallExpression methodCallExpression)
            src\Microsoft.EntityFrameworkCore\Query\ExpressionVisitors\ExpressionVisitorBase.cs(26,0): at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.ExpressionVisitorBase.Visit(Expression node)
            src\Microsoft.EntityFrameworkCore.Relational\Query\RelationalQueryModelVisitor.cs(1021,0): at Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.LiftSubQuery(IQuerySource querySource, Expression itemsExpression, Expression expression)
            src\Microsoft.EntityFrameworkCore.Relational\Query\RelationalQueryModelVisitor.cs(412,0): at Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.CompileMainFromClauseExpression(MainFromClause mainFromClause, QueryModel queryModel)
            src\Microsoft.EntityFrameworkCore\Query\EntityQueryModelVisitor.cs(737,0): at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.VisitMainFromClause(MainFromClause fromClause, QueryModel queryModel)
               at Remotion.Linq.QueryModelVisitorBase.VisitQueryModel(QueryModel queryModel)
            src\Microsoft.EntityFrameworkCore\Query\EntityQueryModelVisitor.cs(718,0): at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.VisitQueryModel(QueryModel queryModel)
            src\Microsoft.EntityFrameworkCore.Relational\Query\RelationalQueryModelVisitor.cs(374,0): at Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitQueryModel(QueryModel queryModel)
            src\Microsoft.EntityFrameworkCore.SqlServer\Query\Internal\SqlServerQueryModelVisitor.cs(92,0): at Microsoft.EntityFrameworkCore.Query.Internal.SqlServerQueryModelVisitor.VisitQueryModel(QueryModel queryModel)
            src\Microsoft.EntityFrameworkCore\Query\EntityQueryModelVisitor.cs(260,0): at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CreateQueryExecutor[TResult](QueryModel queryModel)
            src\Microsoft.EntityFrameworkCore\Storage\Database.cs(67,0): at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](QueryModel queryModel)
            --- End of stack trace from previous location where exception was thrown ---
               at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
            src\Microsoft.EntityFrameworkCore\Query\Internal\QueryCompiler.cs(164,0): at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](Expression query, INodeTypeProvider nodeTypeProvider, IDatabase database, ILogger logger, Type contextType)
            src\Microsoft.EntityFrameworkCore\Query\Internal\QueryCompiler.cs(102,0): at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass15_0`1.<Execute>b__0()
            src\Microsoft.EntityFrameworkCore\Query\Internal\CompiledQueryCache.cs(68,0): at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func`1 compiler)
            src\Microsoft.EntityFrameworkCore\Query\Internal\CompiledQueryCache.cs(45,0): at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
            src\Microsoft.EntityFrameworkCore\Query\Internal\QueryCompiler.cs(99,0): at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
            src\Microsoft.EntityFrameworkCore\Query\Internal\EntityQueryProvider.cs(57,0): at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
               at Remotion.Linq.QueryableBase`1.GetEnumerator()
               at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
               at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
            src\Microsoft.EntityFrameworkCore.Specification.Tests\ComplexNavigationsQueryTestBase.cs(2213,0): at Microsoft.EntityFrameworkCore.Specification.Tests.ComplexNavigationsQueryTestBase`2.Optional_navigation_take_optional_navigation()
            test\Microsoft.EntityFrameworkCore.SqlServer.FunctionalTests\ComplexNavigationsQuerySqlServerTest.cs(1869,0): at Microsoft.EntityFrameworkCore.SqlServer.FunctionalTests.ComplexNavigationsQuerySqlServerTest.Optional_navigation_take_optional_navigation()
      Finished:    Microsoft.EntityFrameworkCore.SqlServer.FunctionalTests
    
  4. maumar commented on Apr 5, 2017

    @maumar
    ContributorAuthor

    This now works in relational but fails for InMemory:

    System.InvalidOperationException : Nullable object must have a value.
    	at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
    	at lambda_method(Closure , TransparentIdentifier`2 )
    	at System.Linq.EnumerableSorter`2.ComputeKeys(TElement[] elements, Int32 count)
    	at System.Linq.EnumerableSorter`1.Sort(TElement[] elements, Int32 count)
    	at System.Linq.OrderedEnumerable`1.<GetEnumerator>d__1.MoveNext()
    	at System.Linq.Enumerable.<TakeIterator>d__24`1.MoveNext()
    	at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
    	C:\git\EntityFramework\src\EFCore\Query\Internal\LinqOperatorProvider.cs(147,0): at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
    	at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
    	at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
    
  5. 2 remaining items

  6. modified the milestones: 2.0.0, on Apr 19, 2017
  7. added a commit that references this issue on May 17, 2017
  8. changed the title [-]Query :: compilation error for queries projecting optional navigation, paging and another projecting of optional navigation[/-] [+]Query: compilation error for queries projecting optional navigation, paging and another projecting of optional navigation[/+] on May 17, 2017
  9. maumar commented on May 17, 2017

    @maumar
    ContributorAuthor

    product issue fixed in earlier checkin, test issue fixed in aba976c

  10. modified the milestones: , 2.0.0 on May 17, 2017
  11. modified the milestones: , 2.0.0 on Oct 15, 2022
  12. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions