Solved

Sum a value in Entity Collection in C#

Posted on 2016-08-26
1
40 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
1 Comment
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
Comment Utility
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.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now