Jul 25 2007

Deffered Execution in DLinq

Category: DLinq | LinqBil@l @ 22:05

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,

Tags: ,

Comments are closed