문제

I'm getting error when I try to execute the last line in this reply. I run the following code:

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));

The count is 1, so the call is performed correctly and yields something. According to the log, piff isn't null, so it contains something. However, after the conversion (which is required in order to access the fields of the entity), I get it to be null (or I get an Exception when trying to explicitly cast the shabang).

The exception is:

Unable to cast object of type 'Microsoft.Crm.SdkTypeProxy.contact' to type 'Microsoft.Crm.Sdk.DynamicEntity'.

What to do?!

도움이 되었습니까?

해결책

The answer is to use RetrieveMultipleRequest. Reworked code:

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));
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top