Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Linq Deferred Execution

0.00/5 (No votes)
17 Feb 2010 1  
To understand how execution take place consider below code: //Query not going to execute at this pointvar query = from customer in db.Customers where customer.City == Paris select customer; Most of the people think that the query gets...
To understand how execution take place consider below code:

//Query not going to execute at this point
var query = from customer in db.Customers  
            where customer.City == "Paris" 
            select customer; 


Most of the people think that the query gets executed at this place but the its not right its get executed when I use query collection for
example

//Query get executed at this point
foreach( customer c in query) 
{
  // your code 
}

Or
int count = (from customer in db.Customers  
            where customer.City == "Paris" 
            select customer).Count();

or to .ToList() or any other function which cause execution.


Consider another scenario

I write code like this

var query = from customer in db.Customers  
            where customer.City == "Paris" 
            select customer;


than change code to below line

query = from customer in db.Customers  
            where customer.City == "Mumbai" 
            select customer; 


and than try to use query object

//it get count of all customer which is related to city Mumbai rather than paris.

int count = query.Count(); 


Find more details[^]

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here