문제

I have a question concerning combine datas from 2 repositorys in asp / mvc.

I have 2 repositories, for example, UserRepo and InvoiceRepo.

Now I want to create a query that contains user data and invoice data (for example: All users with address and data from the last invoice).

What is best way for doing this?

도움이 되었습니까?

해결책 2

Why do you have to do it with one call?

var users = userRepos.GetUsers();
var lastInvoices = invoiceRepos.GetLastInvoiceForAllUsers();
var usersWithInvoices = (from x in users
                         select new UserWithInvoice(x, lastInvoices.First(inv => inv.userId = x.Id);

That will be two SELECTs and must easier to understand.

다른 팁

You can extend an existing repository (probably InvoiceRepo) with a method that performs a join and return all the data you need.

InvoiceRepo.GetUserInvoices(userId) sounds like a sensible option.

This is a common problem with repository pattern - finding a proper repo for your method.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top