LINQ to XML 分组依据

发布于 2024-10-31 07:34:13 字数 1894 浏览 0 评论 0原文

我想使用 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 技术交流群。

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

发布评论

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

评论(3

假情假意假温柔 2024-11-07 07:34:13

如果您不需要可以使用的中间对象

    XDocument input = XDocument.Load("input.xml");
    XDocument output =
        new XDocument(
            new XElement(input.Root.Name,
                from account in input.Root.Elements("Account")
                group account by account.Element("Summary").Value into g
                select new XElement("Account",
                    g.ElementAt(0).Elements().Where(e => e.Name != "Balance"),
                    new XElement("Balance", g.Elements("Balance").Sum(b => (decimal)b)
                    ))));
    output.Save("output.xml");

,或者可以使用方法语法

    XDocument input = XDocument.Load(@"input.xml");
    XDocument output = new XDocument(
            new XElement(input.Root.Name,
                input.Root.Elements("Account")
                .GroupBy(a => a.Element("Summary").Value)
                .Select(g => new XElement("Account",
                    g.ElementAt(0).Elements().Where(e => e.Name != "Balance"),
                    new XElement("Balance", g.Elements("Balance").Sum(b => (decimal)b)
                    )))));
    output.Save("output.xml");

In case you don't want intermediary objects you can use

    XDocument input = XDocument.Load("input.xml");
    XDocument output =
        new XDocument(
            new XElement(input.Root.Name,
                from account in input.Root.Elements("Account")
                group account by account.Element("Summary").Value into g
                select new XElement("Account",
                    g.ElementAt(0).Elements().Where(e => e.Name != "Balance"),
                    new XElement("Balance", g.Elements("Balance").Sum(b => (decimal)b)
                    ))));
    output.Save("output.xml");

or with method syntax you could use

    XDocument input = XDocument.Load(@"input.xml");
    XDocument output = new XDocument(
            new XElement(input.Root.Name,
                input.Root.Elements("Account")
                .GroupBy(a => a.Element("Summary").Value)
                .Select(g => new XElement("Account",
                    g.ElementAt(0).Elements().Where(e => e.Name != "Balance"),
                    new XElement("Balance", g.Elements("Balance").Sum(b => (decimal)b)
                    )))));
    output.Save("output.xml");
有深☉意 2024-11-07 07:34:13

我建议投影到普通对象中,将它们分组,然后投影回 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"))
         )
     );

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"))
         )
     );
幼儿园老大 2024-11-07 07:34:13

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)));

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文