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

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.

n Part 2 of this article series, you looked at Linq's query features, including the standard query operators and their applicability in querying XML data. With that background, this installment switches gears to discuss the basics of DLinq (LINQ to SQL) and its use in querying relational data from a SQL Server database. Specifically, this installment explores leveraging DLinq as a relational query engine and transforming relational output into hierarchical XML output using XLinq. Finally this article also provides an example of using LINQ's mathematical functions for calculating the value of derived columns.

Basics of DLinq
DLinq, a member of the LINQ family of technologies, provides an abstraction layer that lets you treat relational data as classes (also known as entity classes) queryable through LINQ. Inside the abstraction layer, DLinq translates standard LINQ queries into SQL for execution by the database and then translates the tabular results from the database back into objects you define. Your application is then free to manipulate the objects while DLinq stays in the background tracking your changes automatically. Here are the steps for querying relational data using DLinq.

  1. Define and create the entity classes
  2. Reference the entity classes from within your calling application
  3. Write code against the entity classes
What You Need
To follow along and build the downloadable sample code described in this article, you'll need an installed copy of Visual Studio 2005 Professional RTM, and the LINQ May 2006 Community Technology Preview.

Defining Entity Classes
To get started using DLinq you first define entity classes that are linked to relational data, decorating these classes and their properties with special attributes that associate them with specific relational tables and columns. You can define entity classes in three ways:

  1. Using a text editor (such as Notepad) to hand-code the classes manually.
  2. Using a command line utility called sqlmetal.
  3. Using the DLinq designer, which lets you map relational objects to object definitions. With this method, you can create the objects layer as a shareable class library. To begin, create a project using the LINQ Library project template accessible from the New Project dialog box. You can then open the DLinq designer by adding a new item of type "DLinqObjects." Then you can drag and drop elements from the server explorer and map them to the classes defined in the designer.
This article concentrates on creating entity classes using the sqlmetal utility (option 2, above). Installing the May 2006 LINQ CTP edition also installs the sqlmetal utility in the <Drive_Name>\Program Files\LINQ Preview\Bin folder. To create the entity classes (mapped here to the AdventureWorks database), open a command window, change to the sqlmetal install directory, and enter the following command at a command prompt.

   sqlmetal /server:localhost 
    /user:username /password:password  
The preceding command creates a class file named AdventureWorks.cs that contains a number of classes (one class for each table) decorated with the required relational data mapping.

As an example, if you open the file, and look at the declaration of the UnitMeasure class, you'll see a metadata decoration that specify the name of the related database table—UnitMeasure, in this case—and the names of columns that map to class properties.

   public partial class UnitMeasure :  
      private string _UnitMeasureCode;
      private string _Name;
      private System.DateTime _ModifiedDate;
         DBType="NChar(3) NOT NULL", Id=true)]
      public string UnitMeasureCode 
            return this._UnitMeasureCode;
Notice that this approach establishes the appropriate mappings (table/class, column/property, etc) directly within the code. Sometimes, that's convenient, but other times you might want to externalize the mappings, storing them in an external file to separate them from the class implementation. To do this, supply a /map switch to the sqlmetal utility with the name of the XML file that you want to hold the resultant mappings. Doing this allows you to make changes to the mappings on the fly directly in the XML file without having to touch the class implementation.

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