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


What's New in C# 3.0? Part 2 : Page 3

The release of Visual Studio 2008 updates C# to version, 3.0, with several key language enhancements and LINQ support. Part Two of this series will walk you through C#'s new LINQ support features and other time-saving enhancements.

The previous section showed you how to use LINQ to retrieve data from a dataset. A much easier way would be to use LINQ to SQL, which allows you to access SQL databases directly. Here is an example of how it works.

First, add a new item to your project. Select the LINQ to SQL Classes template (see Figure 2). Use the default name of DataClasses1.dbml.

Figure 2. Adding a Template: Adding a LINQ to SQL Classes template to the project.

In Server Explorer, establish a connection to the desired database and drag and drop the table with which you want to work onto the DataClasses1.dbml page (see Figure 3).

Visual Studio 2008 will now create a new class called DataClasses1DataContext (which derives from DataContext). This class allows you to programmatically connect to (and access) the authors table and it's found in the DataClasses1.designer.cs file.

To use LINQ to retrieve the rows in the authors table, first create an instance of the DataClasses1DataContext class:

            DataClasses1DataContext dataContext = 
               new DataClasses1DataContext();
Then, use LINQ to select the required rows:

            var allAuthors = from a in dataContext.authors
                             select a;

Figure 3. Adding a Table: Drag and drop the table you want to use (authors) onto the DataClasses1.dbml page.

Finally, you can print out the individual fields by directly accessing them via the various properties:

            foreach (var a in allAuthors){
                Console.WriteLine("{0} - {1}, {2}", 
                   a.au_id, a.au_fname, a.au_lname);
To retrieve all authors living in Utah and then bind the result to a DataGridView control, use the following statements:

            DataClasses1DataContext dataContext = 
               new DataClasses1DataContext();

            var allAuthors = from a in dataContext.authors
                             where a.state=="UT"
                             select a;

            //---bind to a datagridview control---
            dataGridView1.DataSource = allAuthors;

Another very cool capability of LINQ is its ability to manipulate XML documents. For example, suppose you want to create an XML document by hand, you can use the following code segment:

         XDocument library = new XDocument(
            new XElement("Library",
               new XElement("Book",
                  new XElement("Title", "Professional Windows Vista " +
                               "Gadgets Programming"),
                  new XElement("Publisher", "Wrox")
               new XElement("Book",
                  new XElement("Title", "ASP.NET 2.0 - A Developer's " +
                  new XElement("Publisher", "O'Reilly")
               new XElement("Book",
                  new XElement("Title", ".NET 2.0 Networking Projects"),
                  new XElement("Publisher", "Apress")
               new XElement("Book",
                  new XElement("Title", "Windows XP Unwired"),
                  new XElement("Publisher", "O'Reilly")
Notice that the indentation gives you an overall visualization of the document structure. To save the XML document to file, use the Save() method:

To perform querying on the XML document, load the document from file and then use LINQ:

        XDocument LibraryBooks = new XDocument();
        LibraryBooks = XDocument.Load("Books.xml");

        IEnumerable OReillyBooks = 
           from b in LibraryBooks.Descendants("Book") 
           where b.Element("Publisher").Value == "O'Reilly" 
           select b.Element("Title").Value;

        foreach (var book in OReillyBooks) {
The above code will generate the following output:

ASP.NET 2.0 - A Developer's Notebook
Windows XP Unwired

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