2011-04-09 7 views
5

LINQ를 사용하여 "요약"필드에서 GROUPBY를 수행하고 잔액 필드에서 SUM을 입력 XML을 입력 XML로 변환하려고합니다.LINQ to XML GroupBy

입력 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 :

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

어떤 도움 것 :

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

나는 노드/요소를 얻을이 아니라는 수 시도 감사하겠습니다. 미리 감사드립니다.

답변

3
그때, 그들을 그룹화, 정상 개체로 돌출 된 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")) 
     ) 
    ); 
8

당신이

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

var에 groupData = 새 XElement를 ("루트"를 사용할 수있는 구문 g에 grp.ContractPeriod에 의해 newList 그룹 GRP의 GRP에서새로운 XElement를 ("기간", 새로운 XElement를 ("키", g.Key), 새로운 XElement를 ("하면 FullName", g.Firs을 선택 (). FullName))));

+0

코드를 강조 표시하고 Ctrl + k를 눌러 포맷 할 수 있습니까? – WhatsThePoint