c# - Load child object records with filter condition in Entity Framework -


designed data access layer entity framework , sample poco structure of packageinstance object is

public class packageinstance {   public virtual long packageinstanceid {set;get;}   public virtual boolean isdeleted {set;get;}   public virtual list<session> sessions {set;get;} }  public class session {   public virtual long sessionid {set;get;}   public virtual long packageinstanceid {set;get;}   public virtual boolean isdeleted {set;get;}   public virtual list<note> notes {set;get;} }  public class note {   public virtual long noteid {set;get;}   public virtual long sessionid {set;get;}   public virtual boolean isdeleted {set;get;}   public virtual list<documents> document {set;get;} } 

i need load packageinstance object along child objects in single method call, instead of loading each object separately.

  var packageinstancedb = entity.packageinstances.first(p => p.purchasesessionid == purhcasesessionid);                      //there db call happening here load session.   packageinstancedb.sessions.where(s=>!s.isactive).foreach(s =>    {        //again there db call happening here load associated session notes.        s.notes.where(sn => !sn.isdeleted).tolist().foreach(sd=>        //again there db call happening here load associated note documents.        sd.documents.where(doc=>!doc.isdeleted));   }); 

here how eliminate multiple db calls?

disclaimer: i'm owner of project entity framework plus

ef+ query includefilter feature allow filtering related entities. support ef5

var packageinstancedb = entity.packageinstances     .includefilter(x => x.sessions)     .includefilter(x => x.sessions.select(y => y.notes.where(sn => !sn.isdeleted)))     .includefilter(x => x.sessions.selectmany(y => y.notes.where(sn => !sn.isdeleted)).select(z => z.documents.where(sn => !sn.isdeleted)))     .first(p => p.packageinstanceid == purhcasesessionid); 

note: every path must included

wiki: ef+ query includefilter


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -