Quantcast
Channel: How can I group/sum values in LINQ to XML? - Stack Overflow
Viewing all articles
Browse latest Browse all 2

How can I group/sum values in LINQ to XML?

$
0
0

I've been trying to work thru this but I am having problems grouping the data.I have an XML structure

<FILE><PLAN><PLAN_ID>001</PLAN_ID><PARTICIPANT><PARTICIPANT_ID>9999</PARTICIPANT_ID><INVESTMENTS><INVESTMENT><INVESTMENT_ID>ABC</INVESTMENT_ID><BALANCE>1000</BALANCE><ELECTION>0.00</ELECTION></INVESTMENT><INVESTMENT><INVESTMENT_ID>XYZ</INVESTMENT_ID><BALANCE>2000</BALANCE><ELECTION>0.00</ELECTION></INVESTMENT><INVESTMENT><INVESTMENT_ID>QWERTY</INVESTMENT_ID><BALANCE>3000</BALANCE><ELECTION>100.0</ELECTION></INVESTMENT></INVESTMENTS></PARTICIPANT></PLAN><PLAN><PLAN_ID>002</PLAN_ID><PARTICIPANT><PARTICIPANT_ID>9999</PARTICIPANT_ID><INVESTMENTS><INVESTMENT><INVESTMENT_ID>ABC</INVESTMENT_ID><BALANCE>2000</BALANCE><ELECTION>0.00</ELECTION></INVESTMENT><INVESTMENT><INVESTMENT_ID>XYZ</INVESTMENT_ID><BALANCE>4000</BALANCE><ELECTION>0.00</ELECTION></INVESTMENT><INVESTMENT><INVESTMENT_ID>QWERTY</INVESTMENT_ID><BALANCE>6000</BALANCE><ELECTION>100.0</ELECTION></INVESTMENT></INVESTMENTS></PARTICIPANT></PLAN></FILE>

I started with just trying to get the SUM of all of the BALANCE elements

var doc = XDocument.Load("test.xml");var sum = (from nd in doc.Descendants("BALANCE")           select Int32.Parse(nd.Value)).Sum();Console.WriteLine(sum);

and that worked, giving me 18000. Then I wanted to group the data by the PLAN_ID but I cannot get it to give me other than 0.

var doc = XDocument.Load("test.xml");var q = from x in doc.Descendants("PLAN")        group x by x.Element("PLAN_ID").Value into gr        select new        {             key = gr.Key,             tot = (from tx in gr.Elements("BALANCE")                    select (int)tx).Sum()        };

When I run that I get:

  • [0] { key = "001", tot = 0 }
  • [1] { key = "002", tot = 0 }

Where did I go wrong?


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images