LINQ to XML GroupBy [英] LINQ to XML GroupBy
本文介绍了LINQ to XML GroupBy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望使用LINQ将输入XML转换为输出XML,方法是在摘要字段上执行GROUPBY,然后求出余额字段。
输入XML :
< Root>
<帐户>
<摘要>检查< /摘要>
<综合>兴趣检查帐户< /综合>
<货币>美元< /货币>
<余额> 10000000.000000< /余额>
< /帐户>
<帐户>
<概要>储蓄< /概要>
<综合>市场帐户< /综合>
<货币>美元< /货币>
<余额> 20000000.000000< /余额>
< /帐户>
<帐户>
<摘要>检查< /摘要>
<综合>兴趣检查帐户< /综合>
<货币>美元< /货币>
<余额> 50000000.000000< /余额>
< /帐户>
< / Root>
输出XML:
< Root>
<帐户>
<摘要>检查< /摘要>
<综合>兴趣检查帐户< /综合>
<货币>美元< /货币>
<余额> 60000000.000000< /余额>
< /帐户>
<帐户>
<概要>储蓄< /概要>
<综合>市场帐户< /综合>
<货币>美元< /货币>
<余额> 20000000.000000< /余额>
< /帐户>
< / Root>
我试过了,但无法获取节点/元素:
XElement groupData = new XElement(Root,
chartData.Elements()。GroupBy(x => x.Element(Summary))。价值)。
选择(g =>新XElement(账户,g.Key,g.Elements(综合),
g.Elements(货币),
g.Sum(
s =>
(十进制)
s.Element(Balance)))));
任何帮助将不胜感激。
解决方案
我会建议投影到普通对象中,对它们进行分组,然后重新投影到XML中:
var data =从chartData.Elements()中的acct
选择新的{
Summary =(string)acct.Element (汇总),
综合=(字符串)acct.Element(综合),
货币=(字符串)acct.Element(货币),
余额= )acct.Element(余额),
};
var group =从acct in数据
组acct by acct.Summary进入g
选择新建{
Summary = g.Key,
综合= g.First()综合,
货币= g.First()。综合,
余额= g.Sum(),
};
var groupData = new XElement(Root,
from g in groupped
选择新的XElement(Account,
new XElement(Summary,g .Summary),
新XElement(Comprehensive,g.Comprehensive),
新XElement(Currency,g.Currency),
新XElement(Balance,g.Balance .ToString(0.000000))
)
);
I want to use LINQ to convert input XML to output XML by performing GROUPBY on "Summary" field and SUM up the Balance field.
Input 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>
Output 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>
I tried this but not able to get nodes/elements:
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")))));
Any help would be appreciated. Thanks in advance.
解决方案
I would suggest projecting into normal objects, grouping them, then projecting back into XML:
var data = from acct in chartData.Elements()
select new {
Summary = (string)acct.Element("Summary"),
Comprehensive = (string)acct.Element("Comprehensive"),
Currency = (string)acct.Element("Currency"),
Balance = (decimal)acct.Element("Balance"),
};
var grouped = from acct in data
group acct by acct.Summary into g
select new {
Summary = g.Key,
Comprehensive = g.First().Comprehensive,
Currency = g.First().Comprehensive,
Balance = g.Sum(),
};
var groupData = new XElement("Root",
from g in grouped
select new XElement("Account",
new XElement("Summary", g.Summary),
new XElement("Comprehensive", g.Comprehensive),
new XElement("Currency", g.Currency),
new XElement("Balance", g.Balance.ToString("0.000000"))
)
);
这篇关于LINQ to XML GroupBy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文