0

我试图在共享点列表中获取管理器的值,当管理器值相同时,我试图添加所有金额,然后输出管理器名称和总和。

这是共享点列表中的条目

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 循环以及如何获得预期输出,即每个经理的金额总和。请和谢谢。

4

2 回答 2

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);
    }
}
于 2013-09-12T20:57:27.913 回答
0

您可以使用 LINQGroupBy为您处理管理器的分组,以及对组内所有数量的总和。

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);
于 2013-09-12T21:18:44.750 回答