我正在尝试检索给定CustomerId的UserProfile对象列表。我有theese pocos:
public class UserRole
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserRoleId { get; set; }
public Role Role { get; set; }
public Customer Customer { get; set; }
public virtual UserProfile UserProfile { get; set; }
}
public class Customer
{
public Customer()
{
this.UserRoles = new Collection<UserRole>();
}
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int CustomerId { get; set; }
[Display(Name = "Gruppens namn")]
public string Name { get; set; }
[InverseProperty("Customers")]
public virtual ICollection<UserRole> UserRoles { get; set; }
}
我似乎找不到实际获取UserProfiles的方法。我可以通过以下方式获取UserRoles列表:
dbContext.Customers.First(c => c.CustomerId == customer.CustomerId).UserRoles
但无法访问该对象的UserProfiles。我也试过
dbContext.UserRoles.Where(c => c.Customer.CustomerId == customerId)
但结果相同。想法?
答案 0 :(得分:2)
获取特定客户的配置文件列表:
var result = dbContext.UserRoles
.Where(ur => ur.Customer.CustomerId == customer.CustomerId)
.Select(ur => ur.UserProfile);