c# - Filtering in-memory collection: does IQueryable give better performance? -
let's have in-memory collection(persons) on have apply filter condition.
var distinctproductkeys = persons.where(d => d.productsdictionary != null) .selectmany(d => d.productsdictionary.keys) .distinct(); vs. if use iqueryable before applying filters
var distinctproductkeys = persons.asqueryable() .where(d => d.productsdictionary != null) .selectmany(d => d.productsdictionary.keys) .distinct(); i have read once have collection in-memory, both iqueryable , ienumerable gives same performance.. running both queries observed iqueryable version more fast. can tell me why?
i don't know how testing performance main thing remember when working iqueryable , ienumerable both give deferred execution.
the difference iqueryable interface allows linq-to-sql (linq.-to-anything really) work. if further refine query on iqueryable, query executed in database, if possible.
for ienumerable 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 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 cause of rows loaded in memory.
Comments
Post a Comment