Media_httpfarm4static_ywjli

Explaining LINQ syntax
Originally uploaded by Jane Dallaway

Last night Madgex hosted an excellent VBUG Brighton session by Mike Taulty on LINQ.

Despite the sunny, warm evening we managed to pack 25 or so Microsoft technologies developers into our boardroom and listened intently whilst Mike talked and demo'd his way around LINQ, explaining some of the newer C#/VB9 language features as he went. Whilst not being the exact same slide deck, after a rummage around Mike's site I found a post about a similar sounding talk complete with presentation in PDF format.

I remain slightly dissapointed by the syntax for Linq to XML
var query = from c in data.DescendantsAndSelf("customer")
select (string)c.Attribute("id");

which as Mike said, involves a bit too much of hoping and praying (relying on no underlying changes, no strong typing etc).

However, I'm really encouraged by the idea of Linq to XSD which seems like a much better idea, tying the query to a schema rather than a document.

Fabrice has some sample code based on Linq to XML and Linq to XSD as follows, which goes to show the improvement using the XSD version

Here is a LINQ to XML query:
from item in purchaseOrder.Elements("Item")
select (double)item.Element("Price") * (int)item.Element("Quantity")

Here is the same query as above, but written using LINQ to XSD:
from item in purchaseOrder.Item
select item.Price * item.Quantity

which I think looks much more elegant and less clunky.