I am developing a web application using MVC4 with Entity framework 5.
I have created generic repository for accessing database with unit of work.
Having the below two repository,
These two tables are not linked with each other( there is no foreign key relation ).
Want to write a query by combining these repository. Is possible to write a query by combining two different repository?
If yes, please let me know the way.
If your generic repositories expose an IQueryable method, you should be able to use a LINQ join to query both repositories:
var items = from c in customerRepository.AsQueryable()
join l in libraryRepository.AsQueryable() on c.SomeProperty equals l.SomeOtherProperty
select new { CustomerName = c.FirstName, LibraryName = l.Name };
There may be limitations to what the query can do, but I did a quick proof of concept on my own codebase between two separate repositories and it worked just fine (with the expected sql firing).
Update
It appears you are attempting two separate queries with a where clause-- I don't believe LINQ2Entities supports what you are attempting to do. Try updating your code to the following:
var customer = (
from cus in _customer.Query()
join lib in _library.Query()
on cus.LId equals lib.Id select cus
).ToList();
Where you are replacing the two 'from/where' queries with a 'join/on'.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With