简体   繁体   English

如何获取子节点并将其从父节点分组

[英]how to get the child nodes and group them from a parent node

I have a XElement object that has a structure to the one below. 我有一个XElement对象,其结构如下。 How would I select all the CalcConceptId children nodes from the TestResults root node where the DataPointValue attributes are different, and store them as XElement s in an array/list? 我怎么会选择所有的CalcConceptId从子节点TestResults其中根节点DataPointValue属性是不同的,并存储为XElement在阵列/表S? I want to be able to store each child as another XElement so I can loop through them and fetch out the SeriesAsOfDate and data nodes from each one. 我希望能够将每个子对象存储为另一个XElement以便我可以遍历它们,并从每个子SeriesAsOfDate获取SeriesAsOfDate和数据节点。

<TestResults RSSD="123456">
  <CalcConceptId Id="110" DataPointValue="10">
    <SeriesAsOfDate Value="2013-07-10T00:00:00">
      <Data AsOfDate="7/10/2013" ExpectedValue="1" />
      <Data AsOfDate="7/3/2013" ExpectedValue="14" />
      <Data AsOfDate="6/26/2013" ExpectedValue="55" />
    </SeriesAsOfDate>
  </CalcConceptId>
  <CalcConceptId Id="110" DataPointValue="20">
    <SeriesAsOfDate Value="2013-07-10T00:00:00">
      <Data AsOfDate="7/10/2013" ExpectedValue="4" />
      <Data AsOfDate="7/3/2013" ExpectedValue="34" />
      <Data AsOfDate="6/26/2013" ExpectedValue="1" />
    </SeriesAsOfDate>
  </CalcConceptId>
</TestResults>

I think you want all of the CalcConceptId nodes grouped by DataPointValue but it's a little unclear what "Where the DataPointValue is different" means. 我认为您希望将所有CalcConceptId节点按DataPointValue分组,但是“ DataPointValue的不同之处”的含义还不清楚。

Anyway here is what I think you want... 无论如何,这就是我想要的...

    var calcConceptIdGroupedByDataPointValue =
        doc.Descendants("CalcConceptId")
           .GroupBy(calcConceptId => calcConceptId.Attribute("DataPointValue"));

I'm not 100% sure... but if I get what you're asking, are you looking for this? 我不确定100%...但是,如果我明白您的要求,您是否正在寻找呢?

//assuming the XElement is called Data:
var result = 
    data.Elements().GroupBy(x => int.Parse(x.Attribute("DataPointValue")))
        .Select(g => g.First());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM