1

我正在尝试使用查询表达式获取需要用于更新该字段的数据,以使用发票上的每个行项目更新一个字段(而不覆盖已经存在的内容)。

到目前为止,当只有 1 个订单项存在时,我已经能够让它正常工作。但是,每当我针对多个行项目进行测试时,我都会得到“字典中不存在给定的键”。错误。

任何帮助或推动正确的方向?

    QueryExpression lineitem = new QueryExpression("invoicedetail");
    lineitem.ColumnSet = new ColumnSet("quantity", "productid", "description");
    lineitem.Criteria.AddCondition("invoiceid", ConditionOperator.Equal, invoiceid);

    EntityCollection results = server.RetrieveMultiple(lineitem);
    Invoice.Attributes["aspb_bookmarksandk12educational"] = "Purchases";
    Invoice.Attributes["aspb_bookmarksandk12educational"] += "\n";
    Invoice.Attributes["aspb_bookmarksandk12educational"] += "Product" + "        " + "Quantity";
    Invoice.Attributes["aspb_bookmarksandk12educational"] += "\n";

    foreach (var a in results.Entities)
            {
    string name = a.Attributes["description"].ToString();
    string quantity = a.Attributes["quantity"].ToString();
    Invoice.Attributes["aspb_bookmarksandk12educational"] += " " + name + " ";
    Invoice.Attributes["aspb_bookmarksandk12educational"] += quantity;
    Invoice.Attributes["aspb_bookmarksandk12educational"] += "\n";
    }
4

1 回答 1

1
"The given key was not present in the dictionary."

表明问题在于您尝试访问属性值的方式,而不是返回的多个实体。当您尝试获取属性值时,请尝试在读取值之前检查该属性是否存在,如下所示:

if (a.Attributes.ContainsKey("description"))
{
    var name = a.Attributes["description"] as string;
}

或者甚至更好地使用 SDK 扩展方法来帮助进行检查并为您返回默认值,如下所示:

var name = a.GetAttributeValue<string>("description");
var quantity = a.GetAttributeValue<decimal>("quantity");
于 2016-06-29T18:03:47.257 回答