当我尝试执行this reply中的最后一行时,我收到错误。我运行以下代码:
QueryExpression query = new QueryExpression
{
LogicalName = "contact",
...
}
BusinessEntityCollection response = ServiceProxy.RetrieveMultiple(query);
Logify("count: " + response.BusinessEntities.Count);
BusinessEntity piff= response.BusinessEntities.First();
Logify("piff: " + (piff != null));
DynamicEntity poof = response.BusinessEntities.First() as DynamicEntity;
Logify("poof: " + (poof != null));
计数为1,因此正确执行调用并产生一些效果。根据日志, piff 不是null,所以它包含一些东西。但是,在转换之后(为了访问实体的字段是必需的),我将它变为 null (或者在尝试显式时我得到异常投了shabang)。
例外是:
无法转换类型' Microsoft.Crm.SdkTypeProxy.contact'输入' Microsoft.Crm.Sdk.DynamicEntity'。
怎么办?!
答案 0 :(得分:1)
答案是使用RetrieveMultipleRequest。重写代码:
QueryExpression query = new QueryExpression
{
LogicalName = "contact",
...
}
RetrieveMultipleRequest rmr = new RetrieveMultipleRequest()
{
Query = query,
ReturnDynamicEntities = true
};
RetrieveMultipleResponse rmrresp = ServiceProxy.Execute(rmr) as RetrieveMultipleResponse;
BusinessEntityCollection response = rmrresp.BusinessEntityCollection;
Logify("count: " + response.BusinessEntities.Count);
BusinessEntity piff= response.BusinessEntities.First();
Logify("piff: " + (piff != null));
DynamicEntity poof = response.BusinessEntities.First() as DynamicEntity;
Logify("poof: " + (poof != null));