linq to XML(C#转VB.net转换)

时间:2022-03-09 19:00:18

What is equivalent of below in VB.net

在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()

VB.net Dim list =(from x In xd.Descendants(“product”)。Attributes(“title”)_ Select New(???))。ToList()

Thanks

2 个解决方案

#1


New With { .Title = x.Value }

#2


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)?

你真的想要List(Of)吗?如果您的匿名类型只有一个属性,那么使用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.

这是查询的完整vb.net语法以及一些用于测试它的XML。我通常将.ToList调用分开,但这主要是为了清晰起见。另请注意,使用下面的代码,在.ToList调用之前不会执行查询,因此,为此也可能有助于将它们分开。

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

运行此代码后,ListA的类型为List(Of ),ListB的类型为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

#1


New With { .Title = x.Value }

#2


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)?

你真的想要List(Of)吗?如果您的匿名类型只有一个属性,那么使用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.

这是查询的完整vb.net语法以及一些用于测试它的XML。我通常将.ToList调用分开,但这主要是为了清晰起见。另请注意,使用下面的代码,在.ToList调用之前不会执行查询,因此,为此也可能有助于将它们分开。

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

运行此代码后,ListA的类型为List(Of ),ListB的类型为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