Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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
add comment

1 Answer

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
1  
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
add comment

Your Answer

 
discard

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.