【问题标题】:Selecting Child Node of XElement with LINQ Group By使用 LINQ Group By 选择 XElement 的子节点
【发布时间】:2014-09-21 15:59:39
【问题描述】:

我在表单中有一些 xml:

<Test>
  <Parent Value1="1" Value2="2">
    <Child value3="x"/>
  </Parent>
  <Parent Value1="1" Value2="2">
    <Child value3="y"/>
  </Parent>
  <Parent Value1="a" Value2="b">
    <Child value3="z"/>
  </Parent>
</Test>

我正在尝试使用 xelements 和 linq 将具有相同属性值的父节点下的子节点分组。例如:

<Test>
  <Parent Value1="1" Value2="2">
    <Child value3="x"/>
    <Child value3="y"/>
  </Parent>
  <Parent Value1="a" Value2="b">
    <Child value3="z"/>
  </Parent>
</Test>

到目前为止,这就是我所拥有的:

    Dim l_xeXML As XElement = <Test>
                                  <Parent Value1="1" Value2="2">
                                      <Child value3=""/>
                                  </Parent>
                                  <Parent Value1="1" Value2="2">
                                      <Child value3=""/>
                                  </Parent>
                                  <Parent Value1="a" Value2="b">
                                      <Child value3=""/>
                                  </Parent>
                              </Test>

    Dim l_xeGROUPED As XElement = <Test>
                                      <%= From l_xeExample As XElement In l_xeXML...<Parent>
                                          Group l_xeExample By Key = New With {Key l_xeExample.@Value1, Key l_xeExample.@Value2} Into Group
                                          Select l_GROUPED = New With {.Value1 = Key.Value1, .Value2 = Key.Value2}
                                          Select <Parent Value1=<%= l_GROUPED.Value1 %> Value2=<%= l_GROUPED.Value2 %>>
                                                     <!-- Can't work out what to put here -->
                                                 </Parent> %>

                                  </Test>

    Debug.Print(l_xeGROUPED.ToString)

在&lt;!-- Can't work out what to put here --&gt; 线上,我尝试输入&lt;%= l_xeExample.&lt;Child&gt; %&gt;,但是我收到一条错误消息,指出l_xeExample 不在范围内。

有人可以帮忙吗?

提前致谢

【问题讨论】:

    标签: xml vb.net linq xelement


    【解决方案1】:

    这将产生所需的输出:

    .......
    .......
    Dim l_xeGROUPED As XElement = 
        <Test>
              <%= From l_xeExample As XElement In l_xeXML...<Parent>
                  Group l_xeExample By Key = New With {Key l_xeExample.@Value1, Key l_xeExample.@Value2} 
                  Into Parent = Group
                  Select <Parent Value1=<%= Key.Value1 %> Value2=<%= Key.Value2 %>>
                             <%= From g As XElement In Parent.<Child>
                                 Select g
                             %>
                         </Parent> %>
          </Test>
    Debug.Print(l_xeGROUPED.ToString)
    

    【讨论】:

    • @stormCloud 这种方法有问题吗?
    • 对不起 - 离开了,非常感谢你的方法,它工作正常。答案被接受并赞成:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-03
    • 1970-01-01
    • 2016-11-17
    • 1970-01-01
    • 2011-04-21
    • 1970-01-01
    相关资源
    最近更新 更多