c# - Returning IEnumerable<T> vs. IQueryable<T> -
what difference between returning iqueryable<t>
vs. ienumerable<t>
?
iqueryable<customer> custs = c in db.customers c.city == "<city>" select c; ienumerable<customer> custs = c in db.customers c.city == "<city>" select c;
will both deferred execution , when should 1 preferred on other?
yes, both give deferred execution.
the difference iqueryable<t>
interface allows linq-to-sql (linq.-to-anything really) work. if further refine query on iqueryable<t>
, query executed in database, if possible.
for ienumerable<t>
case, linq-to-object, meaning objects matching original query have loaded memory database.
in code:
iqueryable<customer> custs = ...; // later on... var goldcustomers = custs.where(c => c.isgold);
that code execute sql select gold customers. following code, on other hand, execute original query in database, filtering out non-gold customers in memory:
ienumerable<customer> custs = ...; // later on... var goldcustomers = custs.where(c => c.isgold);
this quite important difference, , working on iqueryable<t>
can in many cases save returning many rows database. prime example doing paging: if use take
, skip
on iqueryable
, number of rows requested; doing on ienumerable<t>
cause of rows loaded in memory.
Comments
Post a Comment