Tuesday, April 29, 2008

LINQ - Paging & Deferred Execution Model


This blog post summarizes the overview of LINQ.

Below are the new features been introduced in Orcas

§ Automatic Properties

§ Object Initializers

§ Collection Initializers

§ Extesnion Methods

§ Lambda Expressions - p => expressions

§ Query Syntax

§ Anonymous Types

§ Var Keyword

These language features help in making query against relational data. This overall querying programming model is called as "LINQ" - which stands for .NET Language Integrated Query.


· LINQ provides built-in support for two extension methods that make the Paging both easy and efficient - the Skip() and Take() methods.

· We can use the Skip() and Take() methods below to indicate that we only want to return 10 product objects - starting at an initial product row that we specify as a parameter argument:

Below is the LINQ code using Paging.

private void QueryProduct()


NorthwindDataContext db = new NorthwindDataContext();

var products = from p in db.MyProducts

where p.Category.CategoryName == "Beverages"

select p;

products = products.Skip(50).Take(10); // Paging

GridView1.DataSource = products;



Deferred Execution Model

· Note above Skip() and Take() operator are added it later to the query (when binding it to my GridView datasource).

· You might think - " Doesn't this mean that the query first grabs all the data from the database and then does the paging in the middle tier (which is bad)?"

· The answer is No. And the reason is that LINQ uses a Deferred Execution Model - which means that the Query Doesn't Actually Execute Until You Try And Iterate over the results.

· One of the benefits of this deferred execution model is that it enables you to nicely compose queries across multiple code statements (which improves code readability). It also enables you to compose queries out of other queries - which enables some very flexible query composition and re-use scenarios.

Reference: file:///D:/Arun.Manglick.EDU/CIPL/Offline%20Pages/Scott%20Guru/LINQ/Part%203.aspx.htm

Hope this helps.

Thanks & Regards,

Arun Manglick || Tech Lead

No comments:

Post a Comment