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

Calling ICollection<>.Contains() with a nested propery throws an InvalidOperationException #4394

Closed
MaxxDelusional opened this issue Jan 25, 2016 · 11 comments
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

@MaxxDelusional
Copy link

The following query will work

long[] teacherIDs = new long[] { 123, 456 };    
var query = from x in context.Students
                   where teacherIDs.Contains(x.TeacherID)
                   select x;

But this query will not

long[] schoolIDs = new long[] { 789, 999 };    
var query = from x in context.Students
                   where schoolIDs.Contains(x.Teacher.SchoolID)
                   select x;

It will throw an InvalidOperationException: Sequence contains more than one element.

Possibly the same as #153

@rowanmiller
Copy link
Contributor

@maumar is this covered by an issue you are working on?

@maumar
Copy link
Contributor

maumar commented Jan 25, 2016

I'm working on optional navigation translation. However this scenario seems to be broken for both optional and required. @mikary fixed a bunch of issues around contains recently, and filed some new ones (eg, #4352, #4355). This might be the same root cause, even though exception is different.

@rowanmiller
Copy link
Contributor

@mikary can you see if this is fixed

@mikary
Copy link
Contributor

mikary commented Feb 3, 2016

I tried to reproduce the issue with Northwind:

        [ConditionalFact]
        public virtual void ContainsRepro()
        {
            using(var context = CreateContext())
            {
                var orderIds = new int[] { 10248, 11077 };

                var query = from x in context.OrderDetails
                            where orderIds.Contains(x.Order.OrderID)
                            select x;

                var y = query.ToList();
            }
        }

It doesn't appear to throw, but it also doesn't use the code path for the issues around contains that I had fixed. This may have been fixed by a different change.

@divega
Copy link
Contributor

divega commented Feb 3, 2016

So we can close it?

@mikary
Copy link
Contributor

mikary commented Feb 3, 2016

The repro looks pretty straight forward and I don't believe there's anything specific to the model that would cause this kind of error.

@mikary mikary closed this as completed Feb 3, 2016
@mikary mikary added the 2 - Done label Feb 3, 2016
@sadjadbp
Copy link

What is the alternative way to perform that query, since RC2 is not coming out soon?

@mikary
Copy link
Contributor

mikary commented Mar 23, 2016

I wasn't able to reproduce the original issue, but it might work if you try with something like this:

long[] schoolIDs = new long[] { 789, 999 };    
var query = from x in context.Students
                   join t in context.Teachers on x.TeacherID equals t.TeacherID
                   where schoolIDs.Contains(t.SchoolID)
                   select x;

If you are still having problems, please provide a reproduction of the issue along with the model and full stack trace so we can better diagnose the problem

@sadjadbp
Copy link

@mikart thanks. I'm sure the bug is there in rc1 so I wait for rc2 to test and repot again. For now I use another way.

@Thealexbarney
Copy link

Here's a complete repro:

using Microsoft.Data.Entity;
using System.Collections.Generic;
using System.Linq;

namespace ContainsBugRepro
{
    class Program
    {
        static void Main(string[] args)
        {
            var ids = new List<int> {1,2,3};

            using (var db = new Context())
            {
                var goodQuery = db.Purchases
                    .Where(x => ids.Contains(x.ItemCount));

                goodQuery.ToList(); //success

                var badQuery = db.Purchases
                    .Where(x => ids.Contains(x.Customer.Age));

                badQuery.ToList(); //exception
            }
        }
    }

    public class Context : DbContext
    {
        public DbSet<Purchase> Purchases { get; set; }
        public DbSet<Customer> Customers { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseInMemoryDatabase();
        }
    }

    public class Purchase
    {
        public int Id { get; set; }
        public int ItemCount { get; set; }
        public int CustomerId { get; set; }
        public Customer Customer { get; set; }
    }

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

@maumar
Copy link
Contributor

maumar commented Apr 7, 2016

@Thealexbarney thanks, I just verified this against our current dev branch and it works (no exception)

@ajcvickers ajcvickers modified the milestones: 1.0.0-rc2, 1.0.0 Oct 15, 2022
@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

8 participants