在 LAMBDA-Expressions 中,如何获取所有用户(姓名和 ID)及其分配的角色(姓名、ID)
我很难弄清楚这个连接的 Lambda 语法!
例如:
UserProfile:
Username, UserId
Ken, 1
Barbie, 2
Alice, 3
webpages_Roles:
RoleName, RoleId
Admin, 1
Editor, 2
Guest, 3
webpages_UsersInRoles
UserId, RoleId
1, 1
1, 2
2, 1
Result should be:
UserName, RoleName, UserId, RoleId
Ken, Admin, 1, 1
Ken, Editor, 1, 2
Barbie, Admin, 2, 1
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public virtual ICollection<UsersInRole> UsersInRole { get; set; }
}
[Table("webpages_Roles")]
public class Role
{
public Role()
{
Members = new List<Membership>();
}
[Key]
public int RoleId { get; set; }
[StringLength(256)]
public string RoleName { get; set; }
public ICollection<Membership> Members { get; set; }
public virtual ICollection<UsersInRole> UsersInRole { get; set; }
}
[Table("webpages_UsersInRoles")]
public class UsersInRole
{
[Key, Column(Order = 0)]
public int UserId { get; set; }
public virtual UserProfile User { get; set; }
[Key, Column(Order = 1)]
public int RoleId { get; set; }
public virtual Role Role { get; set; }
}