我正在尝试使用查询表达式来获取需要用于更新字段的数据,从而使用发票上的每个行项目更新一个字段(而不是覆盖已有的内容)。
到目前为止,我已经能够在只有一个行项目的情况下很好地工作。但是,每当我对多个行项目进行测试时,我都会得到“给定的键不在字典中”。错误。
在正确的方向上有什么帮助或推动吗?
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";
}发布于 2016-06-30 02:03:47
"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");https://stackoverflow.com/questions/38106853
复制相似问题