linq到xml(c#到vb.net的转换)

时间:2022-06-17 20:44:19

What is the VB.net syntax below for?

下面是什么VB.net语法?

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

2 个解决方案

#1


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:

您不需要使用Linq语法,只需使用底层扩展:

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

#2


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

如果您使用VB,那么有一些语法糖:

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.

好的部分是,如果您的文档有一个xsd(并且您可以通过Visual Studio通过一个或几个xml文档推断它来创建一个),您可以像导入名称空间一样导入它,Visual Studio将为您提供编写查询时智能感知完成。

Some references:

#1


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:

您不需要使用Linq语法,只需使用底层扩展:

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

#2


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

如果您使用VB,那么有一些语法糖:

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.

好的部分是,如果您的文档有一个xsd(并且您可以通过Visual Studio通过一个或几个xml文档推断它来创建一个),您可以像导入名称空间一样导入它,Visual Studio将为您提供编写查询时智能感知完成。

Some references: