Sum of column values in sharepoint list if value for another column
matches using C# and CAML
I am trying to get the value of manager in the sharepoint list and when
the manager values are the same, i'm trying to add all the amounts then
output the manager name and the sum.
Here are the entries in the sharepoint list
Manager Amount
1 1000
2 2000
3 3000
4 500
1 1500
2 2500
Then I should send an email that should have this output and return only
the top 3 highest amounts:
Manager Amount
2 4500
3 3000
1 2500
Here is my CAML Query
camlQuery.ViewXml = " <Query><Where><And><IsNotNull><FieldRef
Name='Manager' /></IsNotNull><IsNotNull><FieldRef Name='Amount'
/></IsNotNull></And><OrderBy><FieldRef Name='Amount' Ascending='False'
/></OrderBy></Where></Query> ";
And Here is my Code
double iSum = 0;
foreach (ListItem oListItem in collListItem)
{
FieldUserValue man = (FieldUserValue)oListItem["Manager"];
if(oListItem["Amount"].ToString() == null)
continue;
iSum += Convert.ToDouble(oListItem["Amount"].ToString());
Console.WriteLine("\nManager Name: " + man.LookupValue + "
Amount: " + iSum.ToString());
message += "<tr>"
+ " <td class='pdetails'> " + man.LookupValue + "</td> "
+ " <td class='pdetails'> " + iSum.ToString() +
"</td></tr> ";
}
Kindly help me in fixing my caml query and foreach loop and how to get the
expected output which is the sum of the amounts per manager. Please and
thanks.
No comments:
Post a Comment