c# - Which methods use in Linq to force a retrieving data from database -


when use query, don't retrieve data database:

var query = db.table.where(x => x.id_category.hasvalue).select(x => x.id); 


if use this, i'll retrieve data:

var dataretrieved = query.tolist();   

which others methods, tolist(), can force retrieving data?

linq works building series of query commands, , waits until last possible moment execute them. in order force execution, have ask query "real" data, not description of steps it.

according this msdn article (emphasis mine):

linq queries executed when query variable iterated over, not when query variable created....

to force immediate execution of query not produce singleton value, can call tolist method, todictionary method, or toarray method on query or query variable....

you force execution putting foreach or each loop after query expression, calling tolist or toarray cache data in single collection object.


Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -