LINQ 101

I've been checking out the LINQ technical preview, and it's definitely an eye-opener. The following snippet does a three-way join across an XML data source and two CLR objects. The XML data source is the content of this blog. The objects are a dictionary of date mappings, and an array of strings. The output is constructed as XML.

XDocument doc = XDocument.Load("blog.xml");
 
var d  = new Dictionary<string,string>();
d.Add("2005/09","September 2005");
d.Add("2005/08","August 2005");
 
var a = new string[] { "greasemonkey", "ajax" };
 
var query =
  from
    item in doc.Descendants("item"),
    key in d.Keys,
    tag in a
  where item.Element("date").Value.Contains(key) &&
        item.Element("tags").Value.Contains(tag)
  orderby
    (string) item.Element("date") descending
  select new XElement("item",
    new XElement("month",d[key]),
    item.Element("date"),
    item.Element("title"),
    item.Element("tags"));
 
foreach (var result in query)
  Console.WriteLine(result);

Here's the XML output:

<item>
 <month>September 2005</month>
 <date>2005/09/26</date>
 <title>A channel changer for the Bloglines river of news</title>
 <tags>greasemonkey bloglines</tags>
</item>
<item>
 <month>August 2005</month>
 <date>2005/08/10</date>
 <title>Architecture of participation, architecture of control</title>
 <tags>firefox greasemonkey security opensource</tags>
</item>

The dates in the dictionary constrain the result set to just items in August and September. The strings in the array further constrain it to just items with matching tags.

Could you the same thing in XQuery? Sure, given three sources of equivalent XML data. And in fact the preview includes implementations of the XQuery use cases. LINQ's home base, though, is the domain of CLR objects. From there it extends its generalized query operators into the realms of objects, XML, and relational data. All of the capabilities of the .NET Framework are available within -- and across -- these contexts. What's more, you can invent new query operators using C# and the Framework.

When I said that LINQ turns query inside out, I wasn't kidding. There is a lot here to think about and explore.

Update: Based on email feedback (that was quick!) I should clarify a few things. First, various XQuery implementations do integrate with relational data, though not (yet) with objects. Second, LINQ's (or rather XLINQ's) XML querying is currently an in-memory deal, which means there's a clear opportunity for XQuery engines to plug in underneath the API and deliver real database capability, a la the DLINQ layer which I could alternatively have used to source the above dictionary data from a relational table. Third, I've yet to dig into the tools that create mappings from relational (or XML) type systems to the CLR type system, or the behaviors resulting from those mappings (e.g., statement completion in the IDE, aka IntelliSense), or the mechanisms for proxying database transactions.


Former URL: http://weblog.infoworld.com/udell/2005/09/28.html#a1310