Browse DevX
Sign up for e-mail newsletters from DevX


LINQ Into Microsoft's New Query Capabilities : Page 5

Query features have long been a cornerstone of database applications, but with LINQ, Microsoft introduces query language features right inside of C# and VB.NET.

What DLINQ is to data, XLINQ is to XML, and more. XLINQ provides a handful of extra objects that provide the ability to run standard LINQ queries against XML data sources. Consider the following XML string:

     <companyName>Alfred's Futterkiste</companyName>
     <contactName>Maria Anders</contactName>
     ... More customer records ...
You could load this XML string into an XElement object which you can then use as a LINQ-query data source just like any other object-based data source.

   XElement names = XElement.Parse(xmlString);
   var result = 
      from n in names.Descendants("customer")
      where n.Descendants("companyName")
      select n.Descendants("contactName").Value;
This query returns a list of strings that contains the names of all contacts for customers whose company name starts with "A." As you can see, XLINQ provides objects that represent alternate ways of parsing XML. You can also use these objects outside of LINQ.

XLINQ also provides another interesting feature: The ability to create XML. This is also done using XElement and other XLINQ objects. Consider this XML snippet:

You can create this snippet using XLINQ objects.

   XElement xml = new XElement("root",
      new XElement("sub","Test");
This example creates a new XElement instance by passing the name of the element as the constructor parameter. The content of that element is another XElement, which is also passed to the constructor of the first element. The second element is also instantiated with an element name. The second parameter provides the content, which in this case is the string "Test."

You can use this in queries as well. The next example queries all customers from the USA and returns it as an XElement structure, which in turn can be converted to a string.

   XElement customerXml = new XElement("Customers",
      from c in customers
      where c.Country == "USA"
      select new XElement("Customer",
      new XAttribute("ID",c.CustomerId),
      new XElement("Company",c.CompanyName)));
The result of this example is this:

    <Customer ID="ALFKI">
    <Company>Alfred's Futterkiste</Company>
   <Customer ID="XXX">
     ... More customer records ...
The VB.NET version of LINQ takes this idea even a step further. VB.NET incorporates XML directly into the core language, allowing developers to create the same result in the following fashion:

   Dim x = _
   Select <Customer ID=(c.CustomerId)> 
      </Customer> _
      From c In customers _
      Where c.Country = "USA" _
The support of XML natively in the VB.NET language has been the source of numerous discussions. Some feel that it makes the language "messy." Personally, I like that developers have the choice between the purist, object-oriented approach of C#, and the straightforward, productivity-driven approach of VB.NET.

LINQ is powerful—much more so than can be expressed in a single article. Unfortunately, LINQ also isn't available yet. It is one of those technologies that is so immediately applicable that it is hard to wait for the release version. That release version should also include other functionality such as insert, update, and delete commands. I look forward to all of these things, just as I am looking forward to more information being available for the VB.NET version of LINQ, which appears to be at least as promising as the C# version.

Markus Egger is president of EPS Software Corporation, located in Houston, Texas. He is also the founder of EPS Software Austria, located in Salzburg. He concentrates on consulting in COM-based, object-oriented development and Internet applications. He is an international author and speaker, and is co-publisher of Component Developer Magazine. He is also the author of "Advanced Object-Oriented Programming with Visual FoxPro," from Hentzenwerke Publishing. For the past several years, Markus has received the Microsoft MVP award. Several applications he has worked on (mostly as project manager) have received Microsoft Excellence Award nominations. He is the author of several tools, such as GenRepoX (public domain), the Fox Extension Classes, and Visual WebBuilder.
Thanks for your registration, follow us on our social networks to keep up-to-date