Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Are elements returned by Linq-to-Entities query streamed from the database one at the time ( as they are requested ) or are they retrieved all at once:

    SampleContext context = new SampleContext(); // SampleContext derives from ObjectContext
    var search = context.Contacts;

    foreach (var contact in search)
       Console.WriteLine(contact.ContactID); // is each Contact retrieved from the DB 
                                             // only when foreach requests it?

thank you in advance

share|improve this question
It would be horribly inefficient otherwise... – Simon Whitehead Oct 29 '12 at 21:17

They are retrieved using one query. Your example does simple select for all rows of Contacts table.

Streaming would be highly ineffective.

But this is only for query itself. There might be lazy loading involved for navigation entities stored in properties or collection of entities. Those are loaded on-demand as you access them. This might result in in-famous N+1 problem.

share|improve this answer
I know only one query is sent to the DB, but I remember reading somewhere that results of that query are then streamed one row at the time back to the application – carewithl Oct 29 '12 at 21:17
I apologize, but just to be 100% sure - so results (requested by a single query) are never streamed back as they are requested but are always retrieved at once? – carewithl Oct 29 '12 at 21:41
Sorry. I don't know. Intuitively, I assume that results are all returned in one go, but I might be wrong. – Euphoric Oct 30 '12 at 7:39

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.