c# - Do linq queries re-execute if no changes occured? -


i reading on linq. know there deferred , immediate queries. know deferred types running query when it's enumerated allows changes data set reflected in each enumeration. can't seem find answer if there's mechanism in place prevent query running if no changes occurred data set since last enumeration.

i read on msdn referring linq queries:

therefore, follows if query enumerated twice executed twice.

have overlooked obvious - but...?

indeed, there none. actually, that's not quite true - linq providers spot trivial common examples like:

int id = ... var customer = ctx.customers.singleordefault(x => x.id == id); 

and intercept via identity-manager, i.e. check whether has matching record in context; if does: doesn't execute anything.

you should note re-execution has nothing whether or not data has changed; re-execute (or not) regardless.

there 2 take-away messages here:

  • don't iterate any enumerable more once: not least, isn't guaranteed work @ all
  • if want buffer data, put list/array

so:

var list = somequery.tolist(); 

will never re-execute query, no matter how many times iterate on list. because list not query: a list.

a third take-away be:

  • if have context lives long enough interesting ask data migration, holding data-context far, far long - intended short-lived

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -