Rob*_*ssa 4 c# dynamics-crm dynamics-crm-2011
我正在使用 fetchXML 连接两个实体,并且只能获取第一个列表的结果。我可能犯了一个愚蠢的错误,但这让我折腾了好几个小时。
var query = @"
<fetch version='1.0' mapping='logical' distinct='true'>
<entity name='contact'>
<all-attributes />
<link-entity name='new_custom' from='new_contactid' to='contactid' link-type='inner'>
<all-attributes />
</link-entity>
</entity>
</fetch>
";
var fe = new FetchExpression(query);
var result = service.RetrieveMultiple(fe);
// This contact entity is properly filled
var contact = result.Entities.First().Cast<Contact>();
// This relationship is null, that's unexpected!
var custom = contact.new_contact_new_custom;
// Also null (not sure if this should work)
var related = contact.GetRelatedEntity<new_custom>("new_contact_new_custom", null);
Run Code Online (Sandbox Code Playgroud)
我确实看到在 Contact 实体的Attributes和FormattedValues属性中检索到了正确的数据。但是为什么关系没有正确建立呢?以及如何将此数据转换为正确的“自定义”实体类型?
第一:您确定您的查询返回早期绑定对象吗?在我下面的示例中,我假设您获得 Entity 对象(后期绑定),但对于此示例,它实际上并没有太大区别。
查询的响应基本上是您请求的类型的实体对象的普通表,在本例中为“联系人”。连接实体的字段也可以添加到此表中,但它们包含在AliasedValue对象中。它们的列名以LinkEntity别名为前缀,以防止名称冲突。
您的行var contact = result.Entities.Cast<Contact>();表明该变量contact的类型为Contact,但实际上它是一个IEnumerable<Contact>. (使用var并不总是那么有帮助。)因此,我怀疑您的最后两行甚至无法编译。
在下面的示例中,一个 fetch-query 被发送到 Organization.svc 端点。(注意别名属性。)然后实体“new_custom”的字段“new_name”取自查询结果集的第一行。
var fetchXml = @"
<fetch version='1.0' mapping='logical' distinct='true'>
<entity name='contact'>
<all-attributes />
<link-entity name='new_custom' alias='nc' from='new_contactid' to='contactid' link-type='inner'>
<all-attributes />
</link-entity>
</entity>
</fetch>
";
var query = new FetchExpression(fetchXml);
var response = service.RetrieveMultiple(query);
var contact = response.Entities[0];
var customField = contact.GetAttributeValue<AliasedValue>("nc.new_name");
string name = customField != null ? (string)customField.Value : null;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2390 次 |
| 最近记录: |