【问题标题】:Can't convert nor cast BusinessEntity to DynamicEntity无法将 BusinessEntity 转换或强制转换为 DynamicEntity
【发布时间】:2023-03-17 20:23:01
【问题描述】:

当我尝试执行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 (或者我在尝试显式地显示时得到一个 Exception施放沙梆)。

例外是:

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

怎么办?!

【问题讨论】:

  • 当您使用as <type> 时,只有两种结果。要么类型,要么为空。转换失败,所以poof 为空。您确定 BusinessEntity 可以转换为 DynamicEntity 吗?
  • @paqogomez 我确定这不可能,因为计算机是这样说的。但是,应该作为我链接到的示例工作。在任何一种情况下,我都需要访问获取的实体上的一个字段,如果我不能施放愚蠢的东西,我不知道该怎么做。
  • 2 个问题。首先,BusinessEntity 是否继承自 DynamicEntity。其次,如果你连续两次调用 response.BusinessEntities ,它是否有效?测试第二个问题,你可以先试试 piff 之前的 poof 吗?
  • 你得到的确切异常?
  • @DavidG #1:是的,据我所知。 #2:是的,即使 First 会删除第一个元素,我已经测试了多个实体的响应。

标签: c# dynamics-crm dynamics-crm-4 query-expressions


【解决方案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));

【讨论】:

    猜你喜欢
    • 2015-09-27
    • 1970-01-01
    • 1970-01-01
    • 2018-09-05
    • 2018-05-12
    • 2013-03-07
    • 2015-06-16
    • 2021-06-09
    • 2017-04-28
    相关资源
    最近更新 更多