Reputation: 2655
I have a ManyToMany relationship in my dbContext
modelBuilder.Entity<Role>().HasMany(r => r.Permissions).WithMany(p => p.Roles)
.Map(
m =>
{
m.MapLeftKey("role_id");
m.MapRightKey("per_id");
m.ToTable("roles_permissions");
}
);
the Role.cs looks like:
public class Role
{
[Key]
public int role_Id { get; set; }
public string Name { get; set; }
public ICollection<LoginModel> Users { get; set; }
public ICollection<Permission> Permissions { get; set; }
public Role()
{
Users = new List<LoginModel>();
Permissions = new Collection<Permission>();
}
}
and the Permission.cs looks like:
public class Permission
{
[Key]
public int permi_Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Role> Roles { get; set; }
public Permission()
{
Roles = new Collection<Role>();
}
}
I want to get All the permissions in a list from a specific Role... I'm trying with this
var role = from a in db.Roles
where a.Name.Equals(txt_modificar_nombre.Text)
select a.Permissions;
the role
does not let me get a permission because the type of var role
is:
Can someone help me ?
Those are the values I want to print...
Breakpoint value of var role
Upvotes: 0
Views: 52
Reputation: 236228
You are projecting each matched role into permissions sequence. Thus you have query which returns sequence of sequences. You need to flatten results:
var permissions = from r in db.Roles
where r.Name == txt_modificar_nombre.Text
from p in r.Permissions
select p;
In lambda syntax there is separate operator SelectMany
for projecting and flattening:
var permissions = db.Roles.Where(r => r.Name == txt_modificar_nombre.Text)
.SelectMany(r => r.Permissions);
Upvotes: 1