Skip to content

Query: compilation error for queries streaming correlated collection with FirstOrDefault #11403

Closed

Description

@maumar

reported by @gnaeus

repro:

        using (var ctx = new MyContext())
        {
            var posts = ctx.Users
                .Select(u => u.Posts.Where(p => !p.IsDeleted))
                .FirstOrDefault();
        }

        public class MyContext : DbContext
        {
            public DbSet<User> Users { get; set; }
            public DbSet<Post> Posts { get; set; }

            protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            {
                optionsBuilder.UseSqlServer(@"Server=.;Database=Repro11403;Trusted_Connection=True;MultipleActiveResultSets=True");
            }
        }

        public class User
        {
            public int Id { get; set; }
            public List<Post> Posts { get; set; }
        }

        public class Post
        {
            public int Id { get; set; }
            public bool IsDeleted { get; set; }
        }

QM:

(from User u in DbSet<User>
select 
    from Post p in DbSet<Post>
    where !([p].IsDeleted)
    where  ?= (Nullable<int>)Property([u], "Id") == Property([p], "UserId") =? 
    select [p]).FirstOrDefault()

Exception:

Expression of type 'System.Collections.Generic.IEnumerable`1[Microsoft.EntityFrameworkCore.Storage.ValueBuffer]' cannot be used for parameter of type 'System.Collections.Generic.IEnumerable`1[Microsoft.EntityFrameworkCore.Query.QueryBugsTest+Post]' of method 'System.Collections.Generic.IEnumerable`1[Microsoft.EntityFrameworkCore.Query.QueryBugsTest+Post] _ToEnumerable[Post](System.Collections.Generic.IEnumerable`1[Microsoft.EntityFrameworkCore.Query.QueryBugsTest+Post])'
	at System.Linq.Expressions.Expression.ValidateOneArgument(MethodBase method, ExpressionType nodeKind, Expression arg, ParameterInfo pi)
	at System.Linq.Expressions.Expression.Call(MethodInfo method, Expression arg0)
	at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.ProjectionExpressionVisitor.VisitSubQuery(SubQueryExpression expression)
	at Remotion.Linq.Clauses.Expressions.SubQueryExpression.Accept(ExpressionVisitor visitor)
	at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
	at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.RelationalProjectionExpressionVisitor.Visit(Expression expression)
	at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.VisitSelectClause(SelectClause selectClause, QueryModel queryModel)
	at Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitSelectClause(SelectClause selectClause, QueryModel queryModel)
	at Remotion.Linq.Clauses.SelectClause.Accept(IQueryModelVisitor visitor, QueryModel queryModel)
	at Remotion.Linq.QueryModelVisitorBase.VisitQueryModel(QueryModel queryModel)
	at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.VisitQueryModel(QueryModel queryModel)
	at Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitQueryModel(QueryModel queryModel)
	at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CreateQueryExecutor[TResult](QueryModel queryModel)
	at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](QueryModel queryModel)
	at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](Expression query, IQueryModelGenerator queryModelGenerator, IDatabase database, IDiagnosticsLogger`1 logger, Type contextType)
	at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass13_0`1.<Execute>b__0()
	at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func`1 compiler)
	at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
	at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
	at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
	at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source)

Activity

  1. added this to the 2.1.0 milestone on Mar 23, 2018
  2. smitpatel commented on Mar 23, 2018

    @smitpatel
    Contributor

    This is regression introduced in 2.0.0 release.

  3. added a commit that references this issue on Mar 23, 2018
    fe08f6a
  4. added 3 commits that reference this issue on Mar 27, 2018
    1cb3ad6
    3aa823b
    fc31fa2
  5. modified the milestones: , 2.1.0 on Nov 11, 2019
  6. 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

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions