Solved

Sum a value in Entity Collection in C#

Posted on 2016-08-26
1
75 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 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.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
RLDC Reporting in Visual studio 11 31
vb.net winforms sizing/resolution? 4 45
Upgrading my SSIS package in VS 2012 6 62
MYSQL responding very slow 3 27
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

820 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