简体   繁体   English

实体框架查询

[英]entity framework query

I' ve got tables: Customer, Order, Product 我有桌子:客户,订单,产品

Customer has gor a lot of orders, each order has got a lot wof products. 客户有很多订单,每个订单都有很多产品。

How can I write query to get all products from Customer ? 如何编写查询以从客户那里获得所有产品? I need to use it as a datasource, thanks for help bye 我需要将其用作数据源,谢谢再见

This should work: 这应该工作:

var result = customer
                .SelectMany(x=>x.Orders)
                .Select(x=>x.Products)

Also you can add .Distinct() to retrieve only different products 您也可以添加.Distinct()以仅检索不同的产品

Another way is to go from Products: 另一种方法是转到产品:

var result = dbContext.Products
                .Where(x=>x.Orders.Any(o=>o.Customer.Id == customer.Id))

Hard to read and hard to understand, but still works) 难以阅读且难以理解,但仍然有效)

var products = from customer in customers
               from order in customer.Orders
               from product in order.Products
               select product;

Just use LINQ SelectMany. 只需使用LINQ SelectMany。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM