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
-
It would be horribly inefficient otherwise...Simon Whitehead– Simon Whitehead2012年10月29日 21:17:14 +00:00Commented Oct 29, 2012 at 21:17
1 Answer 1
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.
-
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 applicationcarewithl– carewithl2012年10月29日 21:17:46 +00:00Commented 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– carewithl2012年10月29日 21:41:02 +00:00Commented Oct 29, 2012 at 21:41
-
1Sorry. I don't know. Intuitively, I assume that results are all returned in one go, but I might be wrong.Euphoric– Euphoric2012年10月30日 07:39:40 +00:00Commented Oct 30, 2012 at 7:39