您好我有很多关系设置如下.
public class order { public int id { get; set; } public virtual ICollectionProducts { get; set; } } public class product { public int id { get; set; } public virtual ICollection Orders { get; set; } } public class OrderProducts { public int OrderId { get; set; } public virtual Order Order{ get; set; } public int ProductIdId { get; set; } public virtual Product Product { get; set; } }
我想将所有产品包含(急切加载)到他们的订单中,但是当使用与客户一起显示的相同方法时,我的产品列表会填充OrderProducts-objects而不是Product-objects
public IEnumerableGetAll() { return dataContext.Orders .Include(order => order.Customer) // now include all products aswell.. }
我尝试过任何好运的东西
.Include(order => order.Products.Where(op => op.OrderId == order.Id).Select(p => p.Product))
如果有人可以帮助我,我将非常感激...你也非常欢迎分享任何有关如何构建更高级lambda的好资源,因为我对此并不熟悉.
只需使用ThenInclude()
声明:
public class order { public int id { get; set; } public virtual IListOrderProducts { get; set; } } //... public IEnumerable GetAll() { return dataContext.Orders .Include(order => order.OrderProducts) .ThenInclude(orderProducts => orderProducts.Product); }
如前所述这里,很多一对多的关系尚未实现.你必须加载OrderProducts
,然后Select(orderProducts => orderProducts.Product)
重要提示: IntelliSense完成可能会告诉您无法执行此操作,但您可以,只需进行真正的构建即可