Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Sum a value in Entity Collection in C#

Posted on 2016-08-26
1
Medium Priority
?
127 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 64

Accepted Solution

by:
Fernando Soto earned 2000 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

705 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