1

I have a many-to-many relationship between users and groups and I have a table which can contain a permission for a group. So the entities look something like:

public class Group
{
    public int Id { get; set; }
    public virtual ICollection Users { get; set; }
}

public class User
{
    public int Id { get; set; }
    public virtual ICollection Groups { get; set; }
}

public class Permission
{
    public int Id { get; set; }
    public virtual Group { get; set; }
    public int Value { get; set; }
}

I am wondering how I find out the permissions that are applicable to a user (where the applicable ones are for any groups to which the user belongs).

In the database there will be a mapping table, called UserGroups. If I had access to that, the LINQ query would look something like:

var permissions = 
    from p in MyContext.Permissions
    join m in this.DbContext.UserGroups on p.GroupId equals m.GroupId
    where m.UserId.Equals(theUserId)
    select g;

However (see my related question), since I don't have access to the mapping table, I am not sure the best way to find the applicable permissions. What is the best way to do it?

Thanks for your help, Eric

Community
  • 1
  • 1
Eric
  • 1,945
  • 3
  • 23
  • 33

1 Answers1

2

Try

  var permissions = MyContext.Permissions
               .Where(p => p.Group.Users.Any(u => u.Id == theUserId));
Eranga
  • 32,181
  • 5
  • 97
  • 96