C# 实体框架查询
我有桌子:顾客、订单、产品 客户有很多订单,每个订单都有很多wof产品 如何编写查询以从客户处获取所有产品? 我需要将其用作数据源,谢谢您的帮助 再见这应该可以:C# 实体框架查询,c#,entity-framework-4,C#,Entity Framework 4,我有桌子:顾客、订单、产品 客户有很多订单,每个订单都有很多wof产品 如何编写查询以从客户处获取所有产品? 我需要将其用作数据源,谢谢您的帮助 再见这应该可以: var result = customer .SelectMany(x=>x.Orders) .Select(x=>x.Products) 您还可以添加.Distinct()以仅检索不同的产品 另一种方法是从产品开始: var result = dbCont
var result = customer
.SelectMany(x=>x.Orders)
.Select(x=>x.Products)
您还可以添加.Distinct()以仅检索不同的产品
另一种方法是从产品开始:
var result = dbContext.Products
.Where(x=>x.Orders.Any(o=>o.Customer.Id == customer.Id))
难以阅读和理解,但仍然有效)这应该有效:
var result = customer
.SelectMany(x=>x.Orders)
.Select(x=>x.Products)
您还可以添加.Distinct()以仅检索不同的产品
另一种方法是从产品开始:
var result = dbContext.Products
.Where(x=>x.Orders.Any(o=>o.Customer.Id == customer.Id))
难以阅读和理解,但仍然有效)
只需使用LINQ SelectMany
只需使用LINQ SelectMany。为此编写一个存储过程。为此编写一个存储过程。