我需要根据子元素值对所有值进行分组。下面是我的XML的例子
<Matchday date="2016-05-09">
<Match id="1288348">
<Home id="13" name="club1"/>
<Away id="14" name="club2"/>
<Information>
<country>England</country>
</Information>
</Match>
<Match id="1288349">
<Home id="15" name="club3"/>
<Away id="16" name="club4"/>
<Information>
<country>England</country>
</Information>
</Match>
<Match id="1288350">
<Home id="17" name="club5"/>
<Away id="18" name="club6"/>
<Information>
<country>Italy</country>
</Information>
</Match>
<Match id="1288351">
<Home id="19" name="club7"/>
<Away id="20" name="club8"/>
<Information>
<country>Spain</country>
</Information>
</Match>
</Matchday>
我想按国家/地区分组,因此结果将如下:
England
1288348
1288349
Italy
1288350
Spain
1288351
如何使用vb linq对代码隐藏的xml查询执行此操作。
由于
答案 0 :(得分:3)
这是一种可能的方式:
Dim result = data.Elements("Match") _
.GroupBy(Function(x) x.Element("Information").Element("country").Value)
For Each r As IGrouping(Of String, XElement) In result
Console.WriteLine(r.Key)
For Each m As XElement In r
Console.WriteLine(m.@id)
Next
Next
其中data
是XElement
,声明如下:
Dim data As XElement = <Matchday date="2016-05-09">
<Match id="1288348">
<Home id="13" name="club1"/>
<Away id="14" name="club2"/>
<Information>
<country>England</country>
</Information>
</Match>
<Match id="1288349">
<Home id="15" name="club3"/>
<Away id="16" name="club4"/>
<Information>
<country>England</country>
</Information>
</Match>
<Match id="1288350">
<Home id="17" name="club5"/>
<Away id="18" name="club6"/>
<Information>
<country>Italy</country>
</Information>
</Match>
<Match id="1288351">
<Home id="19" name="club7"/>
<Away id="20" name="club8"/>
<Information>
<country>Spain</country>
</Information>
</Match>
</Matchday>
,输出如下:
England
1288348
1288349
Italy
1288350
Spain
1288351