entity framework - Where in the call chain should Include() EF LINQ extension be positioned? -
i curious in call chain should include called when using entity framework. consider following method:
// sample usage service layer // _customerrepository.paginate(1, out totalrecords, 25, "datejoined desc, amountspent", "datejoined >= '2009-02-01'", "invoices, refunds"); public virtual iqueryable<t> paginate(int page, out int total, int pagesize, string sort = "id", string filter = null, string includes = null) { iqueryable<t> query = databaseset; total = query.count(); // total # of records (required pagination)... var skipto = getvalidskipcount(page, total, pagesize); if (!string.isnullorwhitespace(filter)) { query.where(filter); } // should includes before filtering? // should query.count() called after query.include? // matter? if (!string.isnullorwhitespace(includes)) { query.includemany(includes); // own extension takes comma separated string of entities include } return query.orderby(sort).skip(skipto).take(pagesize); } my questions are:
- should include first in call chain?
- does count() impacted include? if guess should count after include
- should include before or after filtering (where)?
- does matter because ef "smart" enough figure out everything?
should include first in call chain?
include doesn't have first in chain. include special operator not part of query. expansion of query. not linq operator - ef operator defined on objectquery<t> (iqueryable<t> extension , dbquery<t> version both internally implementation). t defines shape of query , root include. include used if resulting shape of query match root used in include call. if add include call on iqueryable<t> , query returns iqueryable<t> include applied if query returns example iqueryable of anonymous type changed shape of query , include not applied.
does count() impacted include? if guess should count after include
include not used if execute count on query. count executes query , doesn't return data record set - changes shape , there nothing include.
should include before or after filtering (where)?
it should not matter. include creates join , expect both ef (when building query) , query engine in database (when executing query) should use filtering prior applying join.
does matter because ef "smart" enough figure out everything?
it matters if change shape of query - use projection or custom join. in such case include of former shape lost (not used @ all).
Comments
Post a Comment