FetchXML强类型相关实体缺少ID

时间:2016-07-08 16:58:42

标签: dynamics-crm crm strong-typing fetchxml

看到这篇关于将结果从FetchXML转换为强类型CRM实体的帖子。

//CrmEntity is a base class I insert into my generated code 
//to differentiate early-bound classes; you can use Entity

public static T ToRelatedEntity<T>(this Entity rawEntity, string relatedAlias)
    where T:CrmEntity, new()        
{
    var result = new Entity(new T().LogicalName);

    foreach(var attribute in rawEntity.Attributes
                                .Where(kvp=>kvp.Key
                                          .StartsWith(relatedAlias + ".")))
    {
        var newName = attribute.Key.Replace(relatedAlias + ".", String.Empty);
        result[newName] = ((AliasedValue)attribute.Value).Value;
    }
    foreach(var formattedValue in rawEntity.FormattedValues
                                    .Where(kvp=>kvp.Key
                                              .StartsWith(relatedAlias + ".")))
    {
        var newName = formattedValue.Key.Replace(relatedAlias + ".", String.Empty);
        result.FormattedValues[newName] = formattedValue.Value;
    }

    return result.ToEntity<T>();
}

//usage
var query = new FetchExpression(fetchXml);
var response = service.RetrieveMultiple(query);
var contact = response.Entities[0].ToEntity<Contact>();
var newCustom = response.Entities[0].ToRelatedEntity<new_custom>("nc");

不幸的是,我需要检索相关的实体ID,并且在FetchXML调用之后不会返回任何ID;只是空Guids。

我可以通过调用result.ID来获取父实体(cc_case)ID。

但是儿童实体怎么样?我如何获得他们的身份证?

编辑:以下是FetchXML的一个示例:

<fetch no-lock="true">
<entity name="cc_case" >
<all-attributes/>
<filter type="and" >
  <condition attribute="cc_caseid" operator="equals" > 
     XXXX
  </condition>
</filter>
<link-entity name="cc_contact_account" from="contactid" to="cc_submitterid" link-type="outer" alias="CC_Contact_Account" >
  <all-attributes/>
  <link-entity name="account" from="accountid" to="accountid" alias="Account" >
    <all-attributes/>
    <link-entity name="contact" from="contactid" to="cc_billingcontactid" alias="Contact" >
      <all-attributes/>
    </link-entity>
  </link-entity>
</link-entity>
<link-entity name="cc_patient" from="cc_patientid" to="cc_patientid" link-type="outer" alias="Patient" >
  <all-attributes/>
</link-entity>

因此,问题是如何从cc_contact_account和cc_patient实体获取ID。我可以从实体cc_case中检索ID,但不能从链接实体中检索ID。

1 个答案:

答案 0 :(得分:0)

来自链接实体的ID应该在AttributeCollection中作为别名属性提供,因此在您的情况下result["Patient.cc_patientid"]会获得患者ID。这就是你的ToRelatedEntity功能正在做的事情。

如果cc_contact_account是多对多关系,我根本不认为会有一个id,但你可以使用与上面相同的别名方法来获取另一个的id。关系的一面。

相关问题