Conditionally Exclude Child Property in Entity Framework Query -
given following object graph
public class bar { public guid id {get;set;} public string somebar {get;set;} } public class foo { public guid id {get;set;} public string foostring {get; set;} public ilist<bar> bars {get;set;} }
how conditionally exclude bars
property entity framework doesn't load entire object graph?
// thoughts public task<foo> getfoobyidasync(guid id, bool includebars) { var query = _db.foos.where(f => f.id == id); if(!includebars) { return query.select(f=> new foo{ id = f.id, foostring = f.foostring }).tolistasync(); } else { return query.tolistasync(); } }
just conditionally add include(f => f.bars) query expression. this:
using system; using system.collections.generic; using system.componentmodel.dataannotations.schema; using system.data.common; using system.data.entity; using system.data.entity.modelconfiguration; using system.data.sqlclient; using system.linq; using system.threading.tasks; namespace consoleapp8 { public class bar { public guid id { get; set; } public string somebar { get; set; } } public class foo { public guid id { get; set; } public string foostring { get; set; } public ilist<bar> bars { get; set; } } class db: dbcontext { public dbset<foo> foos { get; set; } public dbset<bar> bar { get; set; } public async task<foo> getfoobyidasync(guid id, bool includebars) { var query = (iqueryable<foo>)foos.asnotracking(); if (includebars) { query = query.include(f => f.bars); } query = query.where(f => f.id == id); var results = await query.tolistasync(); return results.firstordefault(); } } class program { static void main(string[] args) { database.setinitializer(new dropcreatedatabasealways<db>()); using (var db = new db()) { db.database.initialize(false); db.database.log = m => console.writeline(m); var r1 = db.getfoobyidasync(guid.empty, false).result; var r2 = db.getfoobyidasync(guid.empty, true).result; } console.writeline("hit key exit"); console.readkey(); } } }
outputs
opened connection asynchronously @ 9/11/2017 11:37:26 pm -05:00 select [extent1].[id] [id], [extent1].[foostring] [foostring] [dbo].[foos] [extent1] [extent1].[id] = @p__linq__0 -- p__linq__0: '00000000-0000-0000-0000-000000000000' (type = guid, isnullable = false) -- executing asynchronously @ 9/11/2017 11:37:26 pm -05:00 -- completed in 22 ms result: sqldatareader closed connection @ 9/11/2017 11:37:26 pm -05:00 opened connection asynchronously @ 9/11/2017 11:37:26 pm -05:00 select [project1].[c1] [c1], [project1].[id] [id], [project1].[foostring] [foostring], [project1].[c2] [c2], [project1].[id1] [id1], [project1].[somebar] [somebar] ( select [extent1].[id] [id], [extent1].[foostring] [foostring], 1 [c1], [extent2].[id] [id1], [extent2].[somebar] [somebar], case when ([extent2].[id] null) cast(null int) else 1 end [c2] [dbo].[foos] [extent1] left outer join [dbo].[bars] [extent2] on [extent1].[id] = [extent2].[foo_id] [extent1].[id] = @p__linq__0 ) [project1] order [project1].[id] asc, [project1].[c2] asc -- p__linq__0: '00000000-0000-0000-0000-000000000000' (type = guid, isnullable = false) -- executing asynchronously @ 9/11/2017 11:37:26 pm -05:00 -- completed in 2 ms result: sqldatareader closed connection @ 9/11/2017 11:37:26 pm -05:00 hit key exit
Comments
Post a Comment