LINQ to XML 分组依据
我想使用 LINQ 通过在“Summary”字段上执行 GROUPBY 并对 Balance 字段求和来将输入 XML 转换为输出 XML。
输入 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")))));
任何帮助将不胜感激。提前致谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您不需要可以使用的中间对象
,或者可以使用方法语法
In case you don't want intermediary objects you can use
or with method syntax you could use
我建议投影到普通对象中,将它们分组,然后投影回 XML:
I would suggest projecting into normal objects, grouping them, then projecting back into XML:
var groupData = new XElement("根",
来自 newList 中的 grp
将 grp by grp.ContractPeriod 分组为 g
选择新的 XElement("句点",
新的 XElement("Key", g.Key),
new XElement("FullName", g.Firs().FullName)));
var groupData = new XElement("Root",
from grp in newList
group grp by grp.ContractPeriod into g
select new XElement("Period",
new XElement("Key", g.Key),
new XElement("FullName", g.Firs().FullName)));