Labels

Wednesday, April 30, 2008

LINQ - Custom LINQ Expressions with control

Hi,

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.

Executing Custom SQL Expressions

· One of the best feature of LINQDataSource Control is it supports the SQL Expression very easily as below.

<asp:LinqDataSource ID="LinqDataSource1" runat="server"

ContextTypeName="NorthwindDataContext"

TableName="MyProducts"

EnableDelete="True" EnableInsert="True"

EnableUpdate="True">

</asp:LinqDataSource>

· To handle custom query scenarios we can implement an event handler to handle the "Selecting" event on the <asp:LinqDataSource> control.

o Within this event handler you can write whatever code you want to retrieve a data model result.

o We could do this with a LINQ to SQL query expression, or call a Stored Procedure or use a Custom SQL Expression to retrieve the LINQ to SQL data model.

o Once you retrieve a sequence of data, all you need to-do is to assign it to the "Result" property on the LinqDataSourceSelectEventArgs object. The <asp:LinqDataSource> will then use this sequence as its data to work with.

protected void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e)

{

NorthwindDataContext db = new NorthwindDataContext();

var products = from p in db.MyProducts

where p.Category.CategoryName == "Beverages"

select p;

e.Result = products;

}

Cool Feature:

· Even though we are using a custom Selecting event paging and sorting still work with our GridView. Otherwise, in any datasource control if we provide the Selecting Event then the paging feature does not work automatically and demands to handle PageIndexChanging event to handle.

· This paging and sorting logic happens in the database - which means we are only pulling back the 10 products from the database that we need to display for the current page index in the GridView (making it super efficient).

· You might ask yourself - how is it possible that we get efficient paging and sorting support even when using a custom selecting event? The reason is because 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 out of other queries, and effectively "add-on" behavior to them

Follow reference link.

Reference: http://weblogs.asp.net/scottgu/archive/2007/09/07/linq-to-sql-part-9-using-a-custom-linq-expression-with-the-lt-asp-linqdatasource-gt-control.aspx

Hope this helps.

Thanks & Regards,

Arun Manglick || Tech Lead

No comments:

Post a Comment