ny user-centric application you write will require some sort of data integration—at minimum, you'll need to retrieve some data and display it in the user interface. Often, applications must retrieve data from multiple sources, such as memory collections, relational databases, XML files, and so on. Previous versions of the .NET Framework made getting to this data tedious, and required familiarity with multiple data access technologies and XML APIs.
Even worse, developers had to learn different ways of querying all these data sources: SQL for databases, XQuery for XML, LDAP queries for Active Directory, etc. In short, until now, the data access story has lacked a unified approach to accessing data from disparate data sources. That's exactly the problem Microsoft intends the LINQ (L
uery) family of technologies to solve. LINQ provides a much more unified developer interface for querying data.
You can write LINQ queries and execute them from within your .NET application just by referencing a couple of LINQ assemblies. In addition to invoking LINQ queries programmatically, you can also execute LINQ queries declaratively via a new ASP.NET control—the LinqDataSource control. In this article, you'll see the steps involved in using this new control to access data as well as some more advanced features of the LinqDataSource control, such as passing parameters to a LINQ query and executing a stored procedure. Although this article focuses on using the LINQDataSource control to query a relational database (SQL Server), future articles will discuss how to use LINQ to manage data in other types of stores.
A LinqDataSource Control Primer
You can bind the output of the LinqDataSource control directly to a databinding-capable control such as a GridView or a ListView. You can also configure the LinqDataSource control to implement advanced scenarios such as inserting, updating, and deleting data—without having to write a single line of SQL code.
The LinqDataSource control also provides events that let you handle customized scenarios, such as executing a stored procedure or a complex query. To execute a LINQ query through the LinqDataSource control, you first need to create entity classes that represent the database and its tables. There are two ways you can generate these classes:
- Using the Object Relational Designer: This WYSIWYG interface lets you drag and drop tables, stored procedures, and functions onto the designer surface to create the corresponding entity classes and their methods.
- Using the SqlMetal.exe utility: This command-line utility generates the LINQ object model based on the schema of a supplied database.
To enable this declarative, codeless binding, the LinqDataSource control exposes a number of properties that you'll find familiar if you have worked much with SQL (see Table 1).
Table 1. Common LinqDataSource Control Properties: These properties for enabling declarative, codeless binding should be instantly familiar if you work with SQL.
||The name of the type that exposes a property containing a value you want to access
||Parameter collection used during a DELETE operation
||Properties used to group retrieved data
||Parameter collection used while executing a GROUP BY clause
||Parameter collection used to execute an INSERT operation
||List of fields used to order retrieved data
||Parameter collection for creating the ORDER BY clause
||Fields used to order grouped data
||Parameter collection used to create the ORDER GROUPS BY clause
||List of properties and values to be included in the retrieved data
||Parameter collection used during a SELECT operation
||Name of the property or field in the data context object that specifies the data collection to be queried
||Parameter collection used during an UPDATE operation
||Specifies conditions for a record to be included in the retrieved data
||Parameter collection used during the WHERE clause
With this overview in hand, the next step is to dive right in and build a working example.