linqからxml(c#からvb.netへの変換)
-
03-07-2019 - |
質問
以下のVB.net構文は何のためですか?
var list = xd.Descendants("product")
.Select(element =>new
{
Title = element.Attribute("title").Value,
Duration = element.Element("duration").Value
}).ToList();
解決
これを試してください:
Dim list =
From element In xd.Descendants("product")
Select New With { _
.Title = element.Attribute("title").Value, _
.Duration = element.Element("duration").Value }
Linq構文を使用する必要はありません。基になる拡張機能を使用できます。
Dim list = xd.Descendants("product"). _
Select(Function(element) _
New With { _
.Title = element.Attribute("title").Value, _
.Duration = element.Element("duration").Value _
}). _
ToList()
他のヒント
VBを使用している場合、このための構文糖衣があります:
Dim list =
From element In xd...<product>
Select New With { _
.Title = element.@title, _
.Duration = element.<duration>.Value }
良い点は、ドキュメントのxsdがある場合(および1つまたは複数のxmlドキュメントから推測することでVisual Studioで作成できる)、名前空間とVisual Studioとほぼ同じようにインポートできることです。クエリの作成時にインテリセンスの補完が行われます。
一部の参照:
所属していません StackOverflow