0

当我尝试执行此回复中的最后一行时出现错误。我运行以下代码:

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(或者在尝试显式转换 shabang 时出现异常)。

例外是:

无法将“Microsoft.Crm.SdkTypeProxy.contact”类型的对象转换为“Microsoft.Crm.Sdk.DynamicEntity”类型。

该怎么办?!

4

1 回答 1

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));
于 2014-05-16T05:08:31.817 回答