How to query Entity Framework objects where select is a list

c# entity-framework-6 linq

Question

I'm attempting to do something easy, but I'm having trouble. With the use of EF6, I have two tables connected by a middle associative table, resulting in a many-to-many relationship:

2 associated tables Users and Families

I want to run a query on every user who is a part of a particular family. I thus receive a variety 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 ask everyone who is a member of these families:

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

But I encounter this issue:

Argument for an instance: 'Database.Family[]' cannot be converted to 'System.Linq.IQueryable>'

I appreciate it.

1
1
7/16/2019 9:49:28 AM

Accepted Answer

You can employ any technique:

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

I believe you are looking for this.

2
7/16/2019 9:48:20 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow