LINQ to XML GroupBy

Я хочу использовать LINQ для преобразования входного XML в выходной XML, выполнив GROUPBY в поле «Сводка» и суммируя поле баланса.

Вход XML:

<Root>
  <Account>
    <Summary>Checking</Summary>
    <Comprehensive>Interest Checking Account</Comprehensive>
    <Currency>Dollar</Currency>
    <Balance>10000000.000000</Balance>
  </Account>
  <Account>
    <Summary>Savings</Summary>
    <Comprehensive>Market Account</Comprehensive>
    <Currency>Dollar</Currency>
    <Balance>20000000.000000</Balance>
  </Account>
  <Account>
    <Summary>Checking</Summary>
    <Comprehensive>Interest Checking Account</Comprehensive>
    <Currency>Dollar</Currency>
    <Balance>50000000.000000</Balance>
  </Account>  
</Root>

Выходной XML:

<Root>
  <Account>
    <Summary>Checking</Summary>
    <Comprehensive>Interest Checking Account</Comprehensive>
    <Currency>Dollar</Currency>
    <Balance>60000000.000000</Balance>
  </Account>
  <Account>
    <Summary>Savings</Summary>
    <Comprehensive>Market Account</Comprehensive>
    <Currency>Dollar</Currency>
    <Balance>20000000.000000</Balance>
  </Account>
</Root>

Я пробовал это, но не смог получить узлы / элементы:

XElement groupData = new XElement("Root",
    chartData.Elements().GroupBy(x => x.Element("Summary").Value).
        Select(g => new XElement("Account", g.Key, g.Elements("Comprehensive"),
            g.Elements("Currency"),
            g.Sum(
                s =>
                (decimal)
                s.Element("Balance")))));

Любая помощь будет принята с благодарностью. Заранее спасибо.

5
задан dahlbyk 9 April 2011 в 07:13
поделиться