2

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

1
  • It would be horribly inefficient otherwise... Commented Oct 29, 2012 at 21:17

1 Answer 1

2

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.

3
  • 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
    Commented Oct 29, 2012 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
    Commented Oct 29, 2012 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
    Commented Oct 30, 2012 at 7:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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