我试图在sharepoint列表中获取manager的值,当管理器值相同时,我正在尝试添加所有金额,然后输出管理器名称和总和。
以下是sharepoint列表中的条目
Manager Amount
1 1000
2 2000
3 3000
4 500
1 1500
2 2500
然后我应该发送一封应该具有此输出的电子邮件,并仅返回前3个最高金额:
Manager Amount
2 4500
3 3000
1 2500
这是我的CAML查询
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> ";
这是我的代码
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> ";
}
请帮助我修复我的caml查询和foreach循环以及如何获得预期输出,即每个经理的金额总和。拜托,谢谢。
答案 0 :(得分:0)
我没有方便的SharePoint,所以列表访问代码可能不太正确,但其余的代码应该做你想要的。
ManagerSummary summary = new ManagerSummary();
foreach (ListItem oListItem in collListItem)
{
FieldUserValue managerValue = (FieldUserValue)oListItem["Manager"];
string managerId = managerValue.LookupId;
string managerName = managerValue.LookupValue;
double amount = Convert.ToDouble(oListItem["Amount"].ToString());
summary.AddValue(managerId, managerName, amount);
}
StringBuilder output = new StringBuilder();
output.AppendLine("<tr><th>Manager Name</th><th>Total</th></tr>");
foreach (var manager in summary.TopManagers(3))
{
output.AppendFormat("<tr><td>{0}</td><td>{1}</td></tr>", manager.Name, manager.Total);
output.AppendLine();
}
Console.WriteLine(output.ToString());
class ManagerSummary
{
Dictionary<string, Manager> _managers = new Dictionary<string, Manager>();
public void AddValue(string managerId, string managerName, double amount)
{
if (_managers.ContainsKey(managerId))
{
_managers[managerId].Total += amount;
}
else
{
_managers[managerId] = new Manager { Id = managerId, Name = managerName, Total = amount };
}
}
public List<Manager> TopManagers(int count)
{
var managers = _managers.Values.ToList();
return managers.OrderByDescending(x => x.Total).Take(count).ToList();
}
}
class Manager
{
public string Id { get; set; }
public string Name { get; set; }
public double Total { get; set; }
public override string ToString()
{
return string.Format("{0,-20}{1,-10}", Name, Total);
}
}
答案 1 :(得分:0)
您可以使用LINQ GroupBy
来为您处理管理器分组,以及对组内所有ammounts的求和。
var query = collListItem.OfType<ListItem>()
.Select(item => new
{
Manager = (FieldUserValue)item["Manger"],
Amount = item["Amount"] as double?,
})
.GroupBy(item => item.Manager.LookupValue)
.Select(group => new
{
Manager = group.Key,
Amount = group.Sum(item => item.Amount),
})
.Select(group => string.Format(
@"<tr><td class='pdetails'>{0}</td>
<td class='pdetails'>{1}</td></tr>", group.Manager, group.Amount));
string message = string.Concat(query);