0

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

1
  • Write a stored procedure for this purpose. Commented Dec 1, 2010 at 11:46

2 Answers 2

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

Just use LINQ SelectMany.

Sign up to request clarification or add additional context in comments.

Comments

2

This should work:

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

Also you can add .Distinct() to retrieve only different products

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.