views:

89

answers:

1

Hi folks,

I have a question regarding Linq to XML queries and how we could possibly make them more readable using the new dynamic keyword.

At the moment I am writing things like:

var result = from p in xdoc.Elements("product")
             where p.Attribute("type").Value == "Services"
             select new { ... }

What I would like to write is something like:

var result = from p in xdoc.Products
             where p.Type == "Services"
             select new { ... }

I know I can do this with Linq to XSD which is pretty good already, but obviously this requires an XSD schema and I don't always have one.

I am sure there should be a way to achieve this using the new dynamic features of .NET 4.0 but I'm not sure how or if anyone already had a go at this.

Obviously I would loose some of the advantages of Linq to XSD (typed members and compile time checks) but it wouldn't be worse than the original solution and would certainly be more readable.

Anyone has an idea?

Thanks

+3  A: 

This blog post explores a bit using ExpandoObject in LINQ to XML scenarios.

Stephen Cleary