我想要使用LINQ將輸入XML轉換爲輸出XML,方法是在「摘要」字段上執行GROUPBY,然後求出餘額字段。LINQ to 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")))));
任何幫助將不勝感激。提前致謝。
請問您可以通過突出顯示並按下Ctrl + k來格式化您的代碼 – WhatsThePoint 2017-07-10 13:26:29