我的项目中有一个Employee,customer和customerSelectedCover类。 员工类与客户类具有一对多关系,客户类与customerSelectedCover具有一对多关系。 我还在项目中为Employee创建了一个存储库。 在我的项目中,我通过以下代码获得当前员工
var current = employeeRepository
.FindBy(c => c.UserName == User.Identity.Name)
.Single();
在linq查询中,当前员工obj从db检索所有customerSelectedCover的最佳方法是什么?
答案 0 :(得分:1)
使用Include
方法获取Customers集合,然后使用SelectMany
方法在给定员工的所有客户中投影所有CustomerSelectedCovers,请考虑以下示例代码,模拟您的需求。 (最小的属性用于演示目的)
class Program
{
static void Main(string[] args)
{
Employee CurrentEmployee = new Employee();
List<CustomerSelectedCover> Covers = CurrentEmployee.Customers.
Include("CustomerSelectedCovers").
SelectMany(o => o.CustomerSelectedCovers).ToList();
}
}
class Employee
{
public int Id;
public DbSet<Customer> Customers;
}
class Customer
{
public int Id;
public int EmployeeId;
public DbSet<CustomerSelectedCover> CustomerSelectedCovers;
}
class CustomerSelectedCover
{
public int Id;
public int CustomerId;
}