LINQ to XML 中的分组和求和
给定下面的 XML,我需要按 InputItem 进行分组并对所有年份的成本求和。因此,结果应该是:
- 研究合同、7000 份
- 技术合同、5000 份
- 招待费、2000 份
这是 XML:
<Inputs>
<Inputs_Table_Row>
<InputItem>Research Contracts</InputItem>
<TotalYear1>1000</TotalYear1>
<TotalYear2>2000</TotalYear2>
</Inputs_Table_Row>
<Inputs_Table_Row>
<InputItem>Research Contracts</InputItem>
<TotalYear1>2000</TotalYear1>
<TotalYear2>2000</TotalYear2>
</Inputs_Table_Row>
<Inputs_Table_Row>
<InputItem>Technical Contracts</InputItem>
<TotalYear1>1000</TotalYear1>
<TotalYear2>2000</TotalYear2>
</Inputs_Table_Row>
<Inputs_Table_Row>
<InputItem>Technical Contracts</InputItem>
<TotalYear1>1000</TotalYear1>
<TotalYear2>1000</TotalYear2>
</Inputs_Table_Row>
<Inputs_Table_Row>
<InputItem>Hospitality</InputItem>
<TotalYear1>1000</TotalYear1>
<TotalYear2>1000</TotalYear2>
</Inputs_Table_Row>
</Inputs>
到目前为止,我只是尝试进行分组,但没有成功:
XDocument doc = XDocument.Load(@"c:\temp\CrpTotalsSimple.xml");
var query = from c in doc.Descendants("Inputs_Table_Row")
group new
{
Item = (string)c.Element("InputItem")
}
by c.Element("InputItem")
into groupedData
select new
{
ItemName = groupedData.Key.Value
};
foreach (var item in query)
Console.WriteLine(String.Format("Item: {0}", item.ItemName));
我该如何修复它?
Given the XML below, I need to group by InputItem and sum the costs for all years. So the results should be:
- Research Contracts, 7000
- Technical Contracts, 5000
- Hospitality, 2000
Here's the XML:
<Inputs>
<Inputs_Table_Row>
<InputItem>Research Contracts</InputItem>
<TotalYear1>1000</TotalYear1>
<TotalYear2>2000</TotalYear2>
</Inputs_Table_Row>
<Inputs_Table_Row>
<InputItem>Research Contracts</InputItem>
<TotalYear1>2000</TotalYear1>
<TotalYear2>2000</TotalYear2>
</Inputs_Table_Row>
<Inputs_Table_Row>
<InputItem>Technical Contracts</InputItem>
<TotalYear1>1000</TotalYear1>
<TotalYear2>2000</TotalYear2>
</Inputs_Table_Row>
<Inputs_Table_Row>
<InputItem>Technical Contracts</InputItem>
<TotalYear1>1000</TotalYear1>
<TotalYear2>1000</TotalYear2>
</Inputs_Table_Row>
<Inputs_Table_Row>
<InputItem>Hospitality</InputItem>
<TotalYear1>1000</TotalYear1>
<TotalYear2>1000</TotalYear2>
</Inputs_Table_Row>
</Inputs>
Here's my attempt simply to group so far, but I had no success:
XDocument doc = XDocument.Load(@"c:\temp\CrpTotalsSimple.xml");
var query = from c in doc.Descendants("Inputs_Table_Row")
group new
{
Item = (string)c.Element("InputItem")
}
by c.Element("InputItem")
into groupedData
select new
{
ItemName = groupedData.Key.Value
};
foreach (var item in query)
Console.WriteLine(String.Format("Item: {0}", item.ItemName));
How can I fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你需要类似的东西......
我使用
decimal
因为我怀疑你正在处理可能有小数部分的货币 -int
否则就可以了。You need something like...
I'm using
decimal
because I suspect you're dealing with a currency that may have a fractional component -int
would otherwise be fine.看起来我只是缺少在 group by 子句上使用 Value 。这是我的工作解决方案:
Looks like I was just missing using Value on the group by clause. Here's my working solution: