LINQ to XML 中的分组和求和

发布于 2024-08-04 07:06:05 字数 1869 浏览 6 评论 0原文

给定下面的 XML,我需要按 InputItem 进行分组并对所有年份的成本求和。因此,结果应该是:

  1. 研究合同、7000 份
  2. 技术合同、5000 份
  3. 招待费、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:

  1. Research Contracts, 7000
  2. Technical Contracts, 5000
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

染年凉城似染瑾 2024-08-11 07:06:05

你需要类似的东西......

var query = from yearTotalEl in doc.Descendants()
            where yearTotalEl.Name.LocalName.StartsWith("TotalYear")
            group (decimal)yearTotalEl 
            by (string)yearTotalEl.Parent.Element("InputItem") into g
            select new {ItemName = g.Key, Sum = g.Sum()};

我使用decimal因为我怀疑你正在处理可能有小数部分的货币 - int否则就可以了。

You need something like...

var query = from yearTotalEl in doc.Descendants()
            where yearTotalEl.Name.LocalName.StartsWith("TotalYear")
            group (decimal)yearTotalEl 
            by (string)yearTotalEl.Parent.Element("InputItem") into g
            select new {ItemName = g.Key, Sum = g.Sum()};

I'm using decimal because I suspect you're dealing with a currency that may have a fractional component - int would otherwise be fine.

乞讨 2024-08-11 07:06:05

看起来我只是缺少在 group by 子句上使用 Value 。这是我的工作解决方案:

var query = from c in doc.Descendants("Inputs_Table_Row")
group new
{
    Item = c.Element("InputItem").Value,
    ItemValue = (int)c.Element("TotalYear1") + (int)c.Element("TotalYear2")
 }
 by c.Element("InputItem").Value 
 into groupedData

 select new
 {
      ItemName = groupedData.Key,
      ItemTotal = groupedData.Sum(rec => rec.ItemValue)
  };

Looks like I was just missing using Value on the group by clause. Here's my working solution:

var query = from c in doc.Descendants("Inputs_Table_Row")
group new
{
    Item = c.Element("InputItem").Value,
    ItemValue = (int)c.Element("TotalYear1") + (int)c.Element("TotalYear2")
 }
 by c.Element("InputItem").Value 
 into groupedData

 select new
 {
      ItemName = groupedData.Key,
      ItemTotal = groupedData.Sum(rec => rec.ItemValue)
  };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文