RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


XLinq Part 3: Combining DLinq and XLinq for Query and Display Power : Page 2

DLinq provides mapping capabilities between .NET objects and databases, letting you treat relational data as classes that you can query with LINQ and transform with XLinq to simplify the process of extracting data, storing it in objects, and displaying those objects onscreen.

Executing a Simple DLinq Query
Now that you've seen how DLinq maps classes to tables and properties to columns, here's a simple example that uses DLinq to retrieve relational data, transforms that into XML data using XLinq, and finally displays the XML data by data binding with an XmlDataSource control.

To start, create a new Web site named AdvancedXLinq using the LINQ ASP.NET Web Site template. After creating the Web site, add the AdventureWorks.cs file created in the previous section to the App_Code folder of the new Web site. After that, create a new ASP.NET page and modify its code as shown in Listing 1. Note that in addition to the core LINQ and XLinq namespaces such as System.Query and System.Xml.XLinq, you also need to import the System.Data.DLinq namespace to use DLinq features from the code.

The Page_Load event retrieves the connection string from the Web.config file, passing that to the SqlConnection object constructor:

   SqlConnection connection = new SqlConnection(connectionString);
After creating the SqlConnection object, you supply that as an argument to the constructor of the AdventureWorks class:

   AdventureWorks db = new AdventureWorks(connection);                       
With this plumbing in place, you can now query any table in the AdventureWorks database using the familiar LINQ query notation. As an example, you can retrieve all the categories from the ProductCategory table just by using the format db.Production.ProductCategory. This example retrieves all the categories and transforms them into XML format using the XLinq functional construction technique discussed in Part 1 of this series:

   XElement categories = new XElement("Categories", 
      from category in db.Production.ProductCategory 
      select new XElement("Category",
      new XAttribute("ID", category.ProductCategoryID),
      new XAttribute("Name", category.Name),
      new XAttribute("Guid", category.Rowguid)));
As discussed in Part 2 of this article series, the from…select query retrieves all the categories. You then transform each of the category values into attributes of an XML element named <Category>, which in turn is embedded inside a root <Categories> element. The preceding code generates the following XML:

     <Category ID="1" Name="Bikes" 
       Guid="cfbda25c-df71-47a7-b81b-64ee161aa37c" />
Figure 1. Categories Output: Here's the output of Listing 1, showing the list of categories retrieved from the AdventureWorks database.
To databind the returned XML with an XmlDataSource, set its Data property to the output XML:

     categorySource.Data = categories.Xml; 
Finally you set the DataSource property of the GridView control to the ID of the XmlDataSource and then invoke its DataBind() method:

   gridCategories.DataSource =  categorySource;                       
Figure 1 shows the output produced by the page when requested from the browser.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date