Join multiple table using generic repository patten with Entity framework with unit of work

entity-framework generics repository unit-of-work

Question

I'm use Entity framework 5 and MVC4 to create a web application.

I have developed a general repository for database access using units of labour.

Having the two repositories below

  • Customer table in CustomerRepository
  • Table for a library repository

These two tables are not interconnected in any way ( there is no foreign key relation ).

I want to combine these repositories to create a query. Is it possible to combine two distinct repositories to create a single query?

If so, kindly enlighten me as to how.

1
9
3/12/2014 7:04:57 AM

Accepted Answer

You should be able to use a LINQ join to query both generic repositories if they expose an IQueryable method:

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 };

The query may have some restrictions, but I did a fast proof of concept using my own codebase and two different repositories, and it worked absolutely fine (with the expected sql firing).

Update

You seem to be trying to combine two independent searches with a where clause, which I don't think LINQ2Entities supports. Update your code to something like this:

var customer = (
    from cus in _customer.Query()
    join lib in _library.Query()
    on cus.LId equals lib.Id select cus
).ToList();

Where you are substituting a "join/on" query for the two "from/where" queries.

5
3/14/2014 3:19:50 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow