c# - Entity Framework: Can I make two separate queries in one request? -
c# - Entity Framework: Can I make two separate queries in one request? -
is possible create 2 queries different tables in 1 request?
for example, if have dbcontext:
public dbset<farm> farms { get; set; } public dbset<farmer> farmers { get; set; } public dbset<llama> llamas { get; set; }
and these classes:
public class farm { [key] public guid id { get; set; } public virtual icollection<farmer> farmers { get; set; } public virtual icollection<farmer> llamas { get; set; } } public class farmer { [key] public guid id { get; set; } public guid farmid { get; set; } public virtual farm farm { get; set; } } public class llama { [key] public guid id { get; set; } public guid farmid { get; set; } public virtual farm farm { get; set; } }
would possible create these 2 queries in 1 query?
public ienumerable<object> getinhabitants(guid farmid) { dbcontext db = new dbcontext(); ienumerable<farmer> farmers = db.farmers.where(x => x.farmid == farmid); ienumerable<llama> llamas = db.llamas.where(x => x.farmid == farmid); homecoming [this easier if create 1 query] }
given particular setup, believe do
return db.farmers.where(x => x.farmid == farmid) .union(db.llama.where(x => x.farmid == farmid));
(note: i'm writing off top of head, syntax might bit off).
edited add: might want deferred execution. if using iqueryable object, can create many entity framework requests you'd in code, won't execute them until trigger execution (typically calling .tolist()).
c# linq entity-framework asp.net-mvc-4
Comments
Post a Comment