linq2sql, repository pattern - how to query data from two or more tables?

放肆的年华 提交于 2019-12-04 19:41:47

Think about the statement you made:

But if I want to query all customers that buy specific product by product name, I have to get ProductID of this product using ProductsRep and then get all customers that buy product with this id using CustomersRep.

That smells trouble. Create a repository to facilitate behaviour in a isolated area of your domain, not for each business entity.

A common mistake with DDD/Repository is to think about data. DONT. Think about behaviour.

You have a Products entity which refers to a Customer. Which means a Product cannot exist without a customer. Right? Therefore a Customer is your aggregate root - it controls the behaviour of this portion of your domain model.

Your comment to the answer above confirms that - you are working across multiple repositories. You have two options - 1) unit of work, 2) fix up your repository design.

You only need 1 Repository - let's call it CustomerProductRepository.

Then to do your above query:

var customersWhoBuyHats = customerProductRepository
                             .Products
                             .Include("Customer")
                             .Where(x => x.ProductName == "Hat")
                             .Select(x => x.Customer)
                             .ToList();

The result is a List<Customer>, and you did it with one database call.

HTH.

var customers = from cust in CustomersRep
                join prod in ProductsRep on prod.CustomerID equals cust.CustomerID
                where prod.ProductName == yourInput
                select cust;

You can write a query, similar to you would in SQL (essentially). In the above yourInput is the variable that holds the product name you're interested in.

This should give you a single DB query request, so long as ProductsRep and CustomersRep are in the same DB.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!