c# - Reduce database calls with Entity Framework -
would possible write in 1 statement (make 1 db call?) , still able differentiate between "the member did not exist" , "the member did exist had no dogs".
public ienumerable<dog> getdogsofmember(int id) { if (dbcontext.members.any(i => i.id == id)) { return dbcontext.dogs.where(i => i.member.id == id); } return null; }
if each dog contains reference member, can expose other end of relationship (if didn't already):
public class member { public int id { get; set; } // ... public virtual icollection<dog> dogs { get; set; } } then can issue single efficient query using include():
public ienumerable<dog> getdogsofmember(int id) { var memberwithdogs = dbcontext.members .include(i => i.dogs) .singleordefault(i => i.id == id); if (memberwithdogs != null) return memberwithdogs.dogs; return null; }
Comments
Post a Comment