views:

496

answers:

2

What is the VB.net syntax below for?

   var list = xd.Descendants("product")
   .Select(element =>new 
   { 
      Title = element.Attribute("title").Value,                   
      Duration = element.Element("duration").Value 
   }).ToList();
+1  A: 

Try this:

Dim list = 
   From element In xd.Descendants("product")
   Select New With { _ 
       .Title = element.Attribute("title").Value, _
       .Duration = element.Element("duration").Value }

You don't need to use Linq syntax, you can just use the underlying extensions:

Dim list = xd.Descendants("product"). _
    Select(Function(element) _ 
        New With { _ 
           .Title = element.Attribute("title").Value, _
           .Duration = element.Element("duration").Value _
        }). _
    ToList()
Keith
Thanks Keiths - it sorted me out
Nev_Rahd
+2  A: 

If you are using VB, there is some syntactic sugar for this:

Dim list = 
   From element In xd...<product>
   Select New With { _ 
       .Title = element.@title, _
       .Duration = element.<duration>.Value }

The nice part is that, if you have an xsd for your document (and you can create one through visual studio by inferring it from one or several xml documents), you can import it almost as you would a namespace and Visual Studio will give you intellisense completion when writing your query.

Some references:

Denis Troller
What is the CStr function for? Doesn't element.@title return a String?
Dennis Palmer
That is absolutely true, I don't now why I put it there.
Denis Troller