1

I am trying to do something simple, but I can't figure it out. Using EF6, I have 2 tables which are associated by an intermediate associative table, giving a many-to-many relationship:

2 associated tables Users and Families

I want to query all users who belong to a list of families. So I get an array of families:

var db = new MyProjectEntities(); 
User user = GetUserById((int)HttpContext.Current.Session["CurrentUserId"]);
var families = db.Users.Where(u => u.UserId == user.UserId).First().Families.ToArray();

Then I want to query all users belonging to these families:

var users = db.Users.Where(u => families.Contains(u.Families));

But I get this error:

Instance argument: cannot convert from 'Database.Family[]' to 'System.Linq.IQueryable>'

Thanks in advance.

1 Answer 1

2

You can use Any method:

var users = db.Users.Where(u => u.Families.Any(fam => families.Contains(fam))).AsEnumerable();

I think this is what you're searching for.

Sign up to request clarification or add additional context in comments.

2 Comments

I just had to insert .AsEnumerable() to avoid the 'Unable to create a constant value of type ...' error.
Yea it must be enumerated, sorry for ambiguity

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.