A Look at LINQ's LinqDataSource Control

The LinqDataSource control in .NET 3.5's LINQ library is simpler and quicker to use than older controls, but it's designed for accessing data only in SQL Server databases


December 18, 2007
URL:http://www.drdobbs.com/a-look-at-linqs-linqdatasource-control/205100230

In the .NET Framework 3.5, Language Integrated Query (LINQ) is a set of syntax extensions that add query capabilities to managed languages. Using LINQ, .NET applications can query and transform data using a SQL-like set of operators built into the language syntax and backed up by a new set of framework classes.

Depending on the data being queried, there are distinct flavors of LINQ. They are: Linq-to-SQL, Linq-to-Objects, Linq-to-XML, and Linq-to-DataSets. As the name might suggest, the Linq-to-SQL flavor will query and transform relational data stored in a SQL Server database. Note that this is a SQL Server-only feature and other databases are not supported. In general, Linq-to-SQL can be presented as a higher-level language to query and transform database tables and records. Compared to the SQL Server native T-SQL, Linq-to-SQL employs an object-based vision of the query, manages strongly typed data, and automatically resolves foreign keys and one-to-many relationships between tables. With Linq-to-SQL, you still go through ADO.NET objects and T-SQL strings; however, all of this is hidden from view and managed internally by the framework. From a developer's perspective, Linq-to-SQL is a kind of new query language that is easier to understand and code than T-SQL.

The LinqDataSource control makes LINQ capabilities available to ASP.NET developers through the popular data-source control architecture. Conceptually, the LinqDataSource control is similar to SqlDataSource in that both controls require that you specify the query directly in the markup of the page. It should be noted, though, that LinqDataSource doesn't require connection information. You bind the data source to a dynamically created class&#emdash;the data context class&#emdash;created with the help of a Visual Studio 2008 designer. Compared to ObjectDataSource, the LinqDataSource is simpler and quicker to use, as it doesn't require any manual coding of the business class; at the same time, the requested behavior is expressed through the LINQ language rather than with a method name. Here's how to use the LinqDataSource control:


<asp:LinqDataSource runat="server" ID="LinqDataSource1"
    ContextTypeName="NorthwindDataContext" 
    TableName="Customers" />
<asp:DropDownList runat="server" ID="DropDownList1"
    DataSourceID="LinqDataSource1" />

The ContextTypeName property takes the name of the class generated by the Visual Studio 2008 O/R designer for your data model . The TableName takes the name of the table in that model. The declarative syntax of the LinqDataSource control lets you also specify criteria for displaying, filtering, and ordering data. If the target store is a SQL Server database, you can also configure the control to handle updates, inserts, and deletions of records. More importantly, you can do that without having to write the related SQL commands. Here's a more detailed example:


<asp:LinqDataSource ID="LinqDataSource1" runat="server" 
     ContextTypeName="NorthwindDataContext" 
     TableName="Customers"
     Select="new (CustomerID, CompanyName)"
     OrderBy="customerid, companyname desc"  
     Where="Country == @Country">
     <WhereParameters>
         <asp:ControlParameter Name="Country" 
              Type="String" 
              ControlID="CountryList" 
              PropertyName="SelectedValue" />
     </WhereParameters>
</asp:LinqDataSource>
<asp:DropDownList ID="CustomerList" runat="server"
     AutoPostBack="true"
     DataSourceID="LinqDataSource1" 
     DataTextField="CompanyName"
     DataValueField="CompanyName" />  

The query returns a collection of dynamically created objects that include CustomerID and CompanyName properties. These objects are sorted by ID and company and are filtered by country. The name of the country is selected from a drop-down list named CountryList.

Not all the queries can be expressed as a plain string and assigned to the Select property of the LinqDataSource control. For example, the LINQ syntax fully supports JOINs and DISTINCT queries, but the parser of the string assigned to the Select property just doesn't recognize these operators. The workaround is quite simple and straightforward: you leave the Select property blank and add an event handler for the data source's Selecting event:


<asp:LinqDataSource ID="LinqDataSource1" runat="server" 
     ContextTypeName="NorthwindDataContext" 
     TableName="Customers" 
     OnSelecting="LinqDataSource1_Selecting" />

In the body of the handler, you can use the full LINQ syntax to run the query without being limited by the string format and the capabilities of the embedded data source parser.


private NorthwindDataContext db;
protected void Page_Init(object sender, EventArgs e)
{
    db = new NorthwindDataContext();
}
protected void LinqDataSource1_Selecting(object sender,  LinqDataSourceSelectEventArgs e)
{
    var countries = (from c in db.Customers 
                     select new { c.Country }).Distinct();
    e.Result = countries;
}

The Result property on the LinqDataSourceSelectEventArgs class holds the list of objects that the data source will share with its bound controls.

The pattern behind LinqDataSource and SqlDataSource is nearly the same; the only difference is the language you use&#emdash;Linq-to-SQL in one case, and raw SQL in the other.

Another important difference is that SqlDataSource can accommodate any relational data source, whereas LinqDataSource is limited to SQL Server.

Terms of Service | Privacy Statement | Copyright © 2024 UBM Tech, All rights reserved.