Click here to Skip to main content
14,664,481 members
Rate this:
Please Sign up or sign in to vote.
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

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


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

Rate this:
Please Sign up or sign in to vote.

Solution 2

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

to
.GroupBy(mod => (string)mod.Element("Mod1"))
   
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
   
;)
Rate this:
Please Sign up or sign in to vote.

Solution 1

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
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)
          };
   
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100