【问题标题】:linq to XML (C# to VB.net conversion)linq 到 XML(C# 到 VB.net 的转换)
【发布时间】:2009-04-29 09:54:52
【问题描述】:

在 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") _ 选择新建 ( ??? )).ToList()

谢谢

【问题讨论】:

    标签: linq linq-to-xml


    【解决方案1】:
    New With { .Title = x.Value }
    

    【讨论】:

      【解决方案2】:

      你真的想要一个 List(Of ) 吗?如果您的匿名类型只有一个属性,那么使用 List(Of String) 的结果不是更容易吗?

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

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

      【讨论】:

        猜你喜欢
        • 2010-11-05
        • 2010-10-22
        • 1970-01-01
        • 1970-01-01
        • 2020-02-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-03
        相关资源
        最近更新 更多