Entity Framework - Many to Many Subquery

Posted by Jorin on Stack Overflow See other posts from Stack Overflow or by Jorin
Published on 2010-05-14T22:50:30Z Indexed on 2010/05/14 22:54 UTC
Read the original article Hit count: 244

I asked a question about this previously but my database structure has changed, and while it made other things simpler, now this part is more complicated. Here is the previous question.

At the time, my EF Context had a UsersProjects object because there were other properties. Now that I've simplified that table, it is just the keys, so all my EF context knows about is Users and Projects and the M2M relationship between them. There is no more UsersProjects as far as EF knows.

So my goal is to say "show me all the users who are working on projects with me."

in SQL, this would go something like:

SELECT * FROM Users INNER JOIN UsersProjects ON Users.ID=UsersProjects.UserID
WHERE ProjectID IN (SELECT ProjectID FROM UsersProjects WHERE UserID=@UserID)

and I started in EF with something like this:

            var myProjects =
                (from p in edmx.Projects
                 where p.Users.Contains(edmx.Users.FirstOrDefault(u => u.Email == UserEmail))
                 orderby p.Name
                 select p).ToList();

            var associatedUsers =
                (from u in edmx.Users
                 where myProjects.Contains(?????????)
                 //where myProjects.Any(????????)
                 select u);

The trick is finding what to put in the ????????. Anyone help here?

© Stack Overflow or respective owner

Related posts about entity-framework

Related posts about many-to-many