Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query :: Left Join (SelectMany + GroupJoin + DefaultIfEmpty) doesn't work if the filter is composed on top #3629

Closed
maumar opened this issue Nov 2, 2015 · 1 comment
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@maumar
Copy link
Contributor

maumar commented Nov 2, 2015

Note that this blocks fixing RC1 bug #3186

repro:

    public class Customer
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public int? DetailId { get; set; }
        public CustomerDetail Detail { get; set; }
    }

    public class CustomerDetail
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    public class MyContext : DbContext
    {
        public DbSet<Customer> Customers { get; set; }
        public DbSet<CustomerDetail> CustomerDetails { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(new SqlConnectionStringBuilder { DataSource = ".", InitialCatalog = "Repro3629", MultipleActiveResultSets = true, IntegratedSecurity = true }.ToString());
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Customer>().HasOne(e => e.Detail).WithOne().HasForeignKey<Customer>(e => e.DetailId).IsRequired(false);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            using (var ctx = new MyContext())
            {
                ctx.Database.EnsureDeleted();
                ctx.Database.EnsureCreated();

                var d2 = new CustomerDetail { Name = "D2" };
                var d3 = new CustomerDetail { Name = "D3" };
                var d4 = new CustomerDetail { Name = "D4" };
                var d5 = new CustomerDetail { Name = "D5" };

                var c1 = new Customer { Name = "C1" };
                var c2 = new Customer { Name = "C2", Detail = d2 };
                var c3 = new Customer { Name = "C3", Detail = d3 };
                var c4 = new Customer { Name = "C4", Detail = d4 };

                ctx.CustomerDetails.AddRange(d2, d3, d4, d5);
                ctx.Customers.AddRange(c1, c2, c3, c4);
                ctx.SaveChanges();
            }

            using (var ctx = new MyContext())
            {
                var query = from c in ctx.Customers
                             join d in ctx.CustomerDetails on c.DetailId equals d.Id into grouping
                             from d in grouping.DefaultIfEmpty()
                             where d.Name == "D2"
                             select c;

                var result = query.ToList();

                Console.WriteLine(result);
            }
        }
    }

throws:

Unhandled Exception: System.InvalidOperationException: Sequence contains no elements
at System.Linq.Enumerable.Single[TSource](IEnumerable1 source) at Microsoft.Data.Entity.Query.RelationalQueryModelVisitor.BindMemberOrMethod[TResult](Func4 memberBinder, IQuerySource querySource, IProperty property, Boolean bindSubQueries) in D:\k\EntityFramework\src\EntityFramework.Relational\Query\RelationalQueryModelVisitor.cs:line 907
at Microsoft.Data.Entity.Query.RelationalQueryModelVisitor.<>c__DisplayClass77_01.<BindMemberExpression>b__0(IProperty property, IQuerySource qs) in D:\k\EntityFramework\src\EntityFramework.Relational\Query\RelationalQueryModelVisitor.cs:line 867 at Microsoft.Data.Entity.Query.EntityQueryModelVisitor.<>c__DisplayClass79_01.b__0(IEnumerable1 ps, IQuerySource qs) in D:\k\EntityFramework\src\EntityFramework.Core\Query\EntityQueryModelVisitor.cs:line 1036 at Microsoft.Data.Entity.Query.EntityQueryModelVisitor.BindMemberExpressionCore[TResult](MemberExpression memberExpression, IQuerySource querySource, Func3 memberBinder) in D:\k\EntityFramework\src\EntityFramework.Core\Query\EntityQueryModelVisitor.cs:line 1056
at Microsoft.Data.Entity.Query.EntityQueryModelVisitor.BindMemberExpression[TResult](MemberExpression memberExpression, IQuerySource querySource, Func3 memberBinder) in D:\k\EntityFramework\src\EntityFramework.Core\Query\EntityQueryModelVisitor.cs:line 1031 at Microsoft.Data.Entity.Query.RelationalQueryModelVisitor.BindMemberExpression[TResult](MemberExpression memberExpression, IQuerySource querySource, Func4 memberBinder, Boolean bindSubQueries) in D:\k\EntityFramework\src\EntityFramework.Relational\Query\RelationalQueryModelVisitor.cs:line 866
at Microsoft.Data.Entity.Query.RelationalQueryModelVisitor.BindMemberExpression[TResult](MemberExpression memberExpression, Func4 memberBinder, Boolean bindSubQueries) in D:\k\EntityFramework\src\EntityFramework.Relational\Query\RelationalQueryModelVisitor.cs:line 854 at Microsoft.Data.Entity.Query.ExpressionVisitors.RelationalEntityQueryableExpressionVisitor.VisitMember(MemberExpression memberExpression) in D:\k\EntityFramework\src\EntityFramework.Relational\Query\ExpressionVisitors\RelationalEntityQueryableExpressionVisitor.cs:line 88 at System.Linq.Expressions.MemberExpression.Accept(ExpressionVisitor visitor) at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node) at Microsoft.Data.Entity.Query.ExpressionVisitors.ExpressionVisitorBase.Visit(Expression expression) in D:\k\EntityFramework\src\EntityFramework.Core\Query\ExpressionVisitors\ExpressionVisitorBase.cs:line 15 at System.Linq.Expressions.ExpressionVisitor.VisitBinary(BinaryExpression node) at System.Linq.Expressions.BinaryExpression.Accept(ExpressionVisitor visitor) at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node) at Microsoft.Data.Entity.Query.ExpressionVisitors.ExpressionVisitorBase.Visit(Expression expression) in D:\k\EntityFramework\src\EntityFramework.Core\Query\ExpressionVisitors\ExpressionVisitorBase.cs:line 15 at Microsoft.Data.Entity.Query.EntityQueryModelVisitor.ReplaceClauseReferences(Expression expression, IQuerySource querySource, Boolean inProjection) in D:\k\EntityFramework\src\EntityFramework.Core\Query\EntityQueryModelVisitor.cs:line 920 at Microsoft.Data.Entity.Query.EntityQueryModelVisitor.VisitWhereClause(WhereClause whereClause, QueryModel queryModel, Int32 index) in D:\k\EntityFramework\src\EntityFramework.Core\Query\EntityQueryModelVisitor.cs:line 741 at Microsoft.Data.Entity.Query.RelationalQueryModelVisitor.VisitWhereClause(WhereClause whereClause, QueryModel queryModel, Int32 index) in D:\k\EntityFramework\src\EntityFramework.Relational\Query\RelationalQueryModelVisitor.cs:line 726 at Remotion.Linq.Clauses.WhereClause.Accept(IQueryModelVisitor visitor, QueryModel queryModel, Int32 index) at Remotion.Linq.QueryModelVisitorBase.VisitBodyClauses(ObservableCollection1 bodyClauses, QueryModel queryModel)
at Remotion.Linq.QueryModelVisitorBase.VisitQueryModel(QueryModel queryModel)
at Microsoft.Data.Entity.Query.EntityQueryModelVisitor.VisitQueryModel(QueryModel queryModel) in D:\k\EntityFramework\src\EntityFramework.Core\Query\EntityQueryModelVisitor.cs:line 531
at Microsoft.Data.Entity.Query.RelationalQueryModelVisitor.VisitQueryModel(QueryModel queryModel) in D:\k\EntityFramework\src\EntityFramework.Relational\Query\RelationalQueryModelVisitor.cs:line 255
at Microsoft.Data.Entity.Query.Internal.SqlServerQueryModelVisitor.VisitQueryModel(QueryModel queryModel) in D:\k\EntityFramework\src\EntityFramework.MicrosoftSqlServer\Query\Internal\SqlServerQueryModelVisitor.cs:line 79
at Microsoft.Data.Entity.Query.EntityQueryModelVisitor.CreateQueryExecutor[TResult](QueryModel queryModel) in D:\k\EntityFramework\src\EntityFramework.Core\Query\EntityQueryModelVisitor.cs:line 161
at Microsoft.Data.Entity.Storage.Database.CompileQuery[TResult](QueryModel queryModel) in D:\k\EntityFramework\src\EntityFramework.Core\Storage\Database.cs:line 68
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Microsoft.Data.Entity.Query.Internal.QueryCompiler.<>c__DisplayClass18_01.<CompileQuery>b__0() in D:\k\EntityFramework\src\EntityFramework.Core\Query\Internal\QueryCompiler.cs:line 177 at Microsoft.Data.Entity.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func1 compiler) in D:\k\EntityFramework\src\EntityFramework.Core\Query\Internal\CompiledQueryCache.cs:line 32
at Microsoft.Data.Entity.Query.Internal.QueryCompiler.CompileQuery[TResult](Expression query) in D:\k\EntityFramework\src\EntityFramework.Core\Query\Internal\QueryCompiler.cs:line 134
at Microsoft.Data.Entity.Query.Internal.QueryCompiler.Execute[TResult](Expression query) in D:\k\EntityFramework\src\EntityFramework.Core\Query\Internal\QueryCompiler.cs:line 78
at Microsoft.Data.Entity.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression) in D:\k\EntityFramework\src\EntityFramework.Core\Query\Internal\EntityQueryProvider.cs:line 37
at Remotion.Linq.QueryableBase1.GetEnumerator() at System.Collections.Generic.List1..ctor(IEnumerable1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source)

@maumar
Copy link
Contributor Author

maumar commented Feb 24, 2016

This is fixed, but filter is evaluated on the client, #4588 is tracking that problem

@maumar maumar closed this as completed Feb 24, 2016
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

3 participants