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

asked Oct 29, 2012 at 21:09
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.

answered Oct 29, 2012 at 21:15
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 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? 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. Commented Oct 30, 2012 at 7:39

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.