Entity Framework でデータ アクセス層を設計し、PackageInstance オブジェクトのサンプル POCO 構造は
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;}
}
各オブジェクトを個別にロードするのではなく、単一のメソッド呼び出しで PackageInstance オブジェクトを子オブジェクトと共にロードする必要があります。
var packageInstanceDB = entity.PackageInstances.First(p => p.PurchaseSessionId == purhcaseSessionId);
//There is a DB call happening here to load the Session.
packageInstanceDB.Sessions.Where(s=>!s.IsActive).ForEach(s =>
{
//Again there is a DB call happening here to load the associated session notes.
s.Notes.Where(sn => !sn.IsDeleted).ToList().ForEach(sd=>
//Again there is a DB call happening here to load the associated note documents.
sd.Documents.Where(doc=>!doc.IsDeleted));
});
ここで、複数の DB 呼び出しを排除する方法は?