deferred loading in LINQ
-
hi Lst's we have 2 queries to get data in LInq to sql + asp.net 3.5 //------deferred loading---//
var result = from user in DBContext.Users
select user;foreach (User u in result) { }
//------immediate loading---//
List result = (from user in DBContext.Users
select user).ToList();I want to know -- accrding to defereed loading the queries should be executed only when it's requested in for each loop.and in immediate load we get all data at once. but doesn't query executes on var result =..stmnt.?
-
hi Lst's we have 2 queries to get data in LInq to sql + asp.net 3.5 //------deferred loading---//
var result = from user in DBContext.Users
select user;foreach (User u in result) { }
//------immediate loading---//
List result = (from user in DBContext.Users
select user).ToList();I want to know -- accrding to defereed loading the queries should be executed only when it's requested in for each loop.and in immediate load we get all data at once. but doesn't query executes on var result =..stmnt.?
Member 3981366 wrote:
I want to know -- accrding to defereed loading the queries should be executed only when it's requested in for each loop.and in immediate load we get all data at once.
Yes, you are absolutely correct.Just to be precise, query is executed once and result is then iterated in the foreach loop.
Member 3981366 wrote:
but doesn't query executes on var result =..stmnt.?
No it does not. You have yourself demonstrated it in your examples. To make things more clear, read the "Defered Query Evaluation" section from here[^]