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


What's New in Visual Studio 2008 and the .NET Compact Framework 3.5 : Page 2

The release of Visual Studio 2008 also updates the .NET Compact Framework, with new tools, APIs, LINQ support and WCF features. Find out how these can help make your job easier.

Language Integrated Query (LINQ)
One of the key changes in the .NET Compact Framework is the support for Language Integrated Query (LINQ). LINQ allows you to manipulate data just like you manipulate database records using a query language like SQL. So, instead of writing complex iterative loops to retrieve the data that you desire, you can just specify the data you want declaratively using LINQ and the compiler will do all the work for you.

While .NET Compact Framework 3.5 supports LINQ, some features supported in the .NET Framework are missing, specifically they are:

  • On the .NET Compact Framework, only standard query operators are supported. LINQ to DataSet is supported, which provides LINQ support for DataSet and DataTable.
  • On the .NET Compact Framework, LINQ to XML is supported except for XPath extensions.
To understand the usefulness of LINQ, suppose you have an array of type string that contains a list of names:

        Dim AllNames As String() = _
           {"Jeffrey", "Kirby", "Gabriel", "Philip", "Ross", "Adam", _
            "Alston", "Warren"}
Suppose you need to print out all the names in this array that start with the letter "G". In this case, you can set up a loop and iteratively perform a comparison on each name. Things start to get more complex when you have more complicated filtering rules. Using LINQ, you could specify the filter using the From clause, like this:

        Dim FoundNames As IEnumerable(Of String) = _
           From Name In AllNames Where Name.StartsWith("G")
When this statement has been executed, FoundNames will contain a collection of names that start with the letter "G". It this case, it returns "Gabriel". You can now add the names to a ListBox control, for example:

        For Each PeopleName In FoundNames
You can also use a more complex filter, such as the following:

        Dim FoundNames As IEnumerable(Of String) = _
           From Name In AllNames Where Name.StartsWith("G") Or _
           Name.Contains("by") _
           Order By Name
In this case, FoundNames will now contain "Gabriel, Kirby". Note that result is also sorted.

Another useful application of LINQ is in manipulating Windows Forms controls. Suppose you have a large number of controls on your form and you want to uncheck all the CheckBox controls without setting each individually. You can use the following code to do that using LINQ:

        '---retrieve all the checkbox controls in the current form---
        Dim checkboxes As IEnumerable = _
           From ctrl In Me.Controls Where TypeOf ctrl Is CheckBox

        '---uncheck all the CheckBox controls---
        For Each c As CheckBox In checkboxes
            c.Checked = False
Besides manipulating data in memory, LINQ can also be used to query data stored in structures like datasets and datatables. The following example (in C#) shows how the Employees table within the Northwind.sdf sample database is loaded onto a Dataset object and then queried using LINQ (see Listing 1).

If you want to query the employees based on their hire dates, use the following:

            emp = from employee in ds.Tables[0].AsEnumerable()
                  where employee.Field<DateTime>("Hire Date").Year>1992
                  select employee;
            dataGrid1.DataSource = emp.AsDataView();
Another very cool capability of LINQ is its ability to manipulate XML documents. For example, if you want to create an XML document by hand, you can use the code segment shown in Listing 2.

Notice that the indentation gives you an overall visualization of the document structure. Alternatively, the code in Listing 2 can be rewritten much more clearly in Visual Studio 2008 (only in VB projects):

      Dim library As XElement = _
                    <Title>Professional Windows Vista Gadgets Programming</Title>
                    <Title>ASP.NET 2.0 - A Developer's Notebook </Title>
                    <Title>.NET 2.0 Networking Projects</Title>
                    <Title>Windows XP Unwired</Title>
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:
        Dim LibraryBooks As New XDocument
        LibraryBooks = XDocument.Load("Books.xml")

        Dim OReillyBooks As IEnumerable = _
           From b In LibraryBooks.Descendants("Book") _
           Where b.Element("Publisher") = "O'Reilly" _
           Select b.Element("Title").Value

        For Each 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