Alphard
Alphard

Reputation: 23

How to get the sum of a numeric list from SharePoint?

I'm retrieving numeric values from SharePoint. What I want to do is to add the values from list. However, I'm not sure how to get it while I call the column from SharePoint.

So far I have this:

//Column List from SharePoint which has a numeric value

if (item["ows_Amount_x0020__x0028_LC_x0029_"] != null)
{
  str.AppendLine("<td bgcolor='#FFFFFF';align='right';> " + Convert.ToDecimal(item["ows_Amount_x0020__x0028_LC_x0029_"]).ToString("N0") + "</td>");
}

//Location where I want to put in the Sum
str.AppendLine(" <tr style='color:#ffffff; font-weight:bold'><td bgcolor='#0096D6'>Forecast USD</td></tr>");

Upvotes: 0

Views: 755

Answers (1)

Ryan Erickson
Ryan Erickson

Reputation: 731

Assuming you are looping through each item in the list it will look something like this since there is no support for aggregate functions like SUM with SPQuery.

double total = 0;
foreach(item in list){
  if(item["field"] == null)
     continue;

  total += item["field"];
  str.AppendLine("<td bgcolor='#FFFFFF';align='right';> " + Convert.ToDecimal(item["field"]).ToString("N0") + "</td>");
}
str.AppendLine(" <tr style='color:#ffffff; font-weight:bold'><td bgcolor='#0096D6'>Forecast USD:" + total.toString() + "</td></tr>");

Upvotes: 1

Related Questions