views:

714

answers:

2

What is equivalent of below in VB.net

      var list = (from x in xd.Descendants("product").Attributes("title") select 
      new { Title= x.Value}).ToList();

VB.net Dim list = (From x In xd.Descendants("product").Attributes("title") _ Select New ( ??? )).ToList()

Thanks

A: 
New With { .Title = x.Value }
itowlson
A: 

Do you really want a List(Of )? If your anonymous type only has a single property, wouldn't it be easier to work with a result that was a List(Of String)?

Here's the full vb.net syntax for your query along with some XML to test it with. I generally separate out the .ToList call, but that's mainly for clarity. Also note, that with the code below, the query is not executed until the .ToList call, so it may be helpful to separate them for that reason as well.

After running this code, ListA is type List(Of <anonymous type>) and ListB is type List(Of String)

Dim testXml = <test>
                  <product title="Prod1"/>
                  <product title="Prod2"/>
                  <product title="Prod3"/>
              </test>

Dim queryA = From t In testXml...<product> _
             Select New With {.Title = t.@title}

Dim listA = queryA.ToList

Dim queryB = From t In testXml...<product> _
             Select t.@title

Dim ListB = queryB.ToList
Dennis Palmer