Solved

Sum a value in Entity Collection in C#

Posted on 2016-08-26
1
113 Views
Last Modified: 2016-08-27
Can someone help me figure out how to sum the quantity value here in the EntityCollection in C#?

Thanks in advance.


  string fetchXmlResult =
                             @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false' >
  <entity name='salesorder'> 
                                                 <attribute name='productid'/>
                                                <attribute name='ordernumber'/> 
                                                <attribute name='quantity'/>  
  </entity>
</fetch>";

XmlElement element1= xml.CreateElement(string.Empty, "Order", xml.DocumentElement.NamespaceURI);
           

       EntityCollection xmlResult = _crmservice.RetrieveMultiple(new FetchExpression(fetchXmlResult())); 
 foreach (Entity e in xmlResult.Entities)
                {


                    XmlElement ordernumber= xml.CreateElement("OrderNumber", xml.DocumentElement.NamespaceURI);
                    ordernumber.InnerText =  e.Attributes["ordernumber"]).ToString();
                    element1.AppendChild(ordernumber);

                    XmlElement productid= xml.CreateElement("ProductId", xml.DocumentElement.NamespaceURI);
                    productid.InnerText =  e.Attributes["productid"]).ToString();
                    element1.AppendChild(productid);
}

Open in new window

0
Comment
Question by:teknovation
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 41772602
Hi teknovation;

Declare a int variable outside the foreach loop and increment it by the quantity in the foreach, running total. When the loop is completed you will have the Total.

Have you given any thought of using Linq To XML to work with XML documents, it would make it a little simpler to work with.
1

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question