Simpler Linq to XML queries with the DLR

Posted by Xavier on Stack Overflow See other posts from Stack Overflow or by Xavier
Published on 2010-06-18T02:24:17Z Indexed on 2010/06/18 2:33 UTC
Read the original article Hit count: 503

Filed under:
|
|
|
|

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

© Stack Overflow or respective owner

Related posts about c#

Related posts about Xml