Welcome to Bilal Haidar [MVP, MCT] Official Blog Sign in | Join | Help

Deffered Execution in DLinq

I have started recently working on C# 3.0 Enhancements and LINQ project at the same time. One of the nice features of LINQ is the ability to write Query Expressions. You can think of Query Expressions as a high-level SQL Query. You write a query as follows:

var getCustomers= from c in db.Customers
                            select c;

The above is simply a Query Expression that is stored in-memory, and not yet executed! When it is time to execute such a query, it will be retrieved from the memory, translated into a SQL expression, then executed!

If you want to check the results, you have to execute the query right?
To execute a Query Expression, you have to enumerate through the getCustomers query which is of type IQueryable<Customer>.

foreach (var cust in getCustomers)
{
    Console.WriteLine("Customer ID: {0}", cust.CustomerID);
}

It is only when you loop through the result-set you are actually executing the query!! This is what is known by Deferred Execution.

Hope this helps,
Regards

Published Wednesday, July 25, 2007 12:05 PM by BilalHaidar [MVP]
Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

No Comments

Leave a Comment

(required) 
required 
(required)