chuckd
chuckd

Reputation: 14540

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. How would I select all the CalcConceptId children nodes from the TestResults root node where the DataPointValue attributes are different, and store them as XElements in an array/list? 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.

<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>

Upvotes: 1

Views: 627

Answers (2)

Kevin
Kevin

Reputation: 4636

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.

Anyway here is what I think you want...

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

Upvotes: 1

It&#39;sNotALie.
It&#39;sNotALie.

Reputation: 22794

I'm not 100% sure... but if I get what you're asking, are you looking for this?

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

Upvotes: 0

Related Questions