Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Firends,

I need to group the XML based on Mod1 while summing the group values i need to multiply two elements.
Points to remember
sometimes Mod1 will not be present under Positions. in that case consider it as null

Please Help

XML
<Positions>
  <Office>101</Office>
  <Account>10239</Account>
  <Mod1>STK</Mod1>
  <AmountClosingPrice>10.00</AmountClosingPrice>
  <Quantity>500.00</Quantity>
</Positions>

<Positions>
  <Office>101</Office>
  <Account>10239</Account>
  <Mod1>STK</Mod1>
  <AmountClosingPrice>20.00</AmountClosingPrice>
  <Quantity>200.00</Quantity>
</Positions>

  <Positions>
    <Office>101</Office>
    <Account>10239</Account>
    <Mod1>MUN</Mod1>
    <AmountClosingPrice>10.00</AmountClosingPrice>
    <Quantity>200.00</Quantity>
  </Positions>

    <Positions>
      <Office>101</Office>
      <Account>10239</Account>
      <Mod1>MUN</Mod1>
      <AmountClosingPrice>5.00</AmountClosingPrice>
      <Quantity>200.00</Quantity>
    </Positions>

      <Positions>
        <Office>101</Office>
        <Account>10239</Account>
        <AmountClosingPrice>10.00</AmountClosingPrice>
        <Quantity>10.00</Quantity>
      </Positions>

      <Positions>
        <Office>101</Office>
        <Account>10239</Account>
        <Mod1>MUN</Mod1>
        <AmountClosingPrice>5.00</AmountClosingPrice>
        <Quantity>100.00</Quantity>
      </Positions>
      <Positions>


C#
docm=XDocument.Parse(xmlDocumentWithoutNs.ToString());
           var result = docm.Descendants("Positions")
                               .GroupBy(mod => mod.Element("Mod1"))
                               .Select(s=> new{
                                   Mod=s.Key,
                                   Count=s.Count(),
                                   mktval =s.Sum(q=>(decimal?)q.Element("Quantity") * (decimal?)q.Element("AmountClosingPrice"))
                               })
                               .ToList();



Expected Result

STK 2 9000
MUN 2 3000
Null 2 600

Thanks
Posted
Updated 1-Aug-15 10:15am
v4

change
C#
.GroupBy(mod => mod.Element("Mod1")) 

to
C#
.GroupBy(mod => (string)mod.Element("Mod1"))
 
Share this answer
 
Comments
jinesh sam 2-Aug-15 10:29am    
Thanks...Its helps me:)
DamithSL 2-Aug-15 10:32am    
You are welcome!
Maciej Los 3-Aug-15 2:40am    
5ed!
DamithSL 3-Aug-15 2:43am    
After Long Time :)

Thank You Maciej
Maciej Los 3-Aug-15 3:49am    
;)
I think one easy way is to do this in two phases. The first query will convert the data from XML to variable data types and the second does the grouping based on the first query.

So consider something like the following
C#
var intermediate = from item in  docm.Descendants("Positions")
                  select new {
                     Mod = (string)item.Element("Mod1"),
                     Quantity = (decimal?)item.Element("Quantity"),
                     AmountClosingPrice = (decimal?)item.Element("AmountClosingPrice")
                  };

var result = from item in intermediate
          group item by item.Mod into groupedItem
          select new {
             Mod = groupedItem.Key,
             mktval = groupedItem.Sum(item => item.Quantity * item.AmountClosingPrice)
          };
 
Share this answer
 
Comments
jinesh sam 2-Aug-15 10:30am    
Thanks for your support...:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900