Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to create a Generic Repository a 'deep include'?
    text
    copied!<p>I have created a Generic repository with some methods on top of EF Codefirst, which works great. The Get method looks like this (this has include for one level)</p> <pre><code>public virtual IEnumerable&lt;T&gt; Get( Expression&lt;Func&lt;T, bool&gt;&gt; filter = null, Func&lt;IQueryable&lt;T&gt;, IOrderedQueryable&lt;T&gt;&gt; orderBy = null, string includeProperties = "") { IQueryable&lt;T&gt; query = _dDbSet; if (filter != null) query = query.Where(filter); query = includeProperties.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Aggregate(query, (current, includeProperty) =&gt; current.Include(includeProperty)); return orderBy != null ? orderBy(query).ToList() : query.ToList(); } </code></pre> <p>But I stumble on a problem when I need the generic repository to return a 'deeper' include</p> <p>An example: A Hotel object have a relation to a list of rooms and each room have a relation to person.</p> <p>I want my method to return a Hotel object with a list of rooms with the person of those rooms related.</p> <p>How can I solve this by using a GenericRepository?</p> <p>I do not want to use the solution:</p> <pre><code> var hotel = _hotelRepository.Context.Hotels.Where(p =&gt; p.HotelId == HotelId).Include(p =&gt; p.RoomList.Select(b =&gt; b.Person)).First(); </code></pre> <p>in my bussinesclass because I want to seperate out the data access to the genericRepository and be able to Mock out the method in my test project.</p> <p>Is there a good solution for this, or do I have to use the context with a Select as mentioned above?</p> <p>Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload