title: Cant use Linq WHERE/IN/JOIN with Entity Framework on many to many
body: <p>I can do this very simply in SQL, but trying to learn how to do this in Linq using Entity Framework.</p>
  
  <p>I have a User, a User can have multiple Groups, and a group can have multiple Permissions.</p>
  
  <p>I would like to get all the permissions for a user.</p>
  
  <p>I have the following DbSets in my DbContext</p>
  
  <pre><code>public virtual DbSet&lt;User&gt; Users { get; set; }
  public virtual DbSet&lt;Group&gt; Groups { get; set; }
  public virtual DbSet&lt;Permission&gt; Permissions { get; set; }
  </code></pre>
  
  <p>And there classes are like so</p>
  
  <pre><code>public class User : Entity, IUser&lt;long&gt;
  {
      public string UserName { get; set; }
  
      public string PasswordHash { get; set; }
  
      public User()
          : base()
      {
          this.Groups = new HashSet&lt;UserGroup&gt;();
      }
  
      public virtual ICollection&lt;UserGroup&gt; Groups { get; set; }
  }
  
  public class Group : AuditableEntity
  {
      public Group() {
          this.Permissions = new HashSet&lt;GroupPermission&gt;();            
      }
  
      public string Name { get; set; }
  
      public virtual ICollection&lt;GroupPermission&gt; Permissions { get; set; }
  }
  
  public class Permission 
  {
      public string Name { get; set; }
  
      public Permission()
      {
  
      }
  }
  </code></pre>
  
  <p>To cope with the many to many relationships, i also have these classes</p>
  
  <pre><code>public class UserGroup
  {
  
      public long UserId { get; set; }
      public long GroupId { get; set; }
  
      public virtual User User { get; set; }
      public virtual Group Group { get; set; }
  
  }
  
  public class GroupPermission
  {
      public long PermissionId { get; set; }
      public long GroupId { get; set; }
  
      public virtual Permission Permission { get; set; }
      public virtual Group Group { get; set; }
  }
  </code></pre>
  
  <p>Now in SQL i would simply do </p>
  
  <pre><code>SELECT Permission.* 
  FROM Permission
      INNER JOIN PermissionGroup ON Permission.Id = PermissionGroup.PermissionId
      INNER JOIN Group ON PermissionGroup.GroupId = Group.Id
      INNER JOIN UserGroup ON Group.Id = UserGroup.GroupId
  WHERE UserGroup.UserId = @UserId
  </code></pre>
  
  <p>Can someone please tell me why i cant do this in LINQ?  Well i know i can, but i cannot figure out how.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>context.PermissionGroup and context.UserGroup are not DbSets in my context.</p>
  
  <pre><code>List&lt;Permission&gt; result;
  using(DataContext context = new DataContext())
  {
     result = (from permission in context.Permissions
               join permissionGroup in context.PermissionGroup on permission.Id equals permissionGroup.Id
               join g in context.Groups on permissionGroup.GroupId equals g.Id
               join userGroup in context.UserGroup on g.Id equals userGroup.GroupId
               where userGroup.UserId == user.Id 
               select permission).ToList();
  }
  </code></pre>