2

我正在尝试检索给定 CustomerId 的 UserProfile 对象列表。我有这些 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)

但同样的结果。想法?

4

1 回答 1

2

要获取特定客户的配置文件列表:

var result = dbContext.UserRoles
                   .Where(ur => ur.Customer.CustomerId == customer.CustomerId)
                   .Select(ur => ur.UserProfile);
于 2013-10-18T12:55:11.403 回答