在展开查询中选择之前进行 OData 过滤

OData filtering before selecting inside an expand query

我正在为 Dynamics 365 中的元数据编写查询,但我认为这个问题对于任何 OData 查询都是普遍存在的。

我的问题如下:

在 Dynamics 中,我们可以在实体中拥有多种类型的字段,例如字符串、布尔值或查找。我想编写一个查询 returns 查找所寻址的实体类型。

{url}/api/data/v9.0/EntityDefinitions(LogicalName='Account')
?$select=
    LogicalName
&$expand=
    Attributes(
        $select=
            LogicalName,
            AttributeType,
            Targets; -- Problematic property
        $filter=
      AttributeType+eq+Microsoft.Dynamics.CRM.AttributeTypeCode'Lookup')

如果我在此查询中不包含 Targets 的 select,我会得到正确的结果,即 AttributeType 为 Lookup 的所有属性。

但是当我要求同时包含目标时,我收到一条错误消息

Could not find a property named 'Targets' on type 'Microsoft.Dynamics.CRM.AttributeMetadata

因为目标 属性 仅存在于那些类型为 Lookup 的数组中,因此 select 从字符串属性中获取此列将失败并引发此错误。

有没有办法在 select 列之前先过滤查找?我发现 here 求值顺序是

$filter、$inlinecount、$orderby、$skiptoken、$skip、$top、$expand、$select、$format

这正是我所需要的,只是我不认为在 $expand 属性中调用时这个顺序是相同的。

为了获得查找实体,您可能更幸运地在 ManyToOneRelationships 上执行 $expand 而不是 Attributes 并获得 ReferencedEntity 值。

像这样的东西应该可以工作:
.../api/data/v9.1/EntityDefinitions(LogicalName='account') ?$select=LogicalName &$expand=ManyToOneRelationships($select=ReferencingAttribute,ReferencedEntity)

结果子集:

{"@odata.context":"https://myOrg.api.crm.dynamics.com/api/data/v9.1/$metadata#EntityDefinitions(LogicalName,ManyToOneRelationships(ReferencingAttribute,ReferencedEntity))/$entity","LogicalName":"account","MetadataId":"70816501-edb9-4740-a16c-6a5efbc05d84","ManyToOneRelationships":[{"ReferencingAttribute":"msdyn_accountkpiid","ReferencedEntity":"msdyn_accountkpiitem","MetadataId":"2a712c96-09b1-e811-a842-000d3a33bdbd"},{"ReferencingAttribute":"preferredequipmentid","ReferencedEntity":"equipment","MetadataId":"b4b462b5-ee78-467d-a97a-45264d234816"},{"ReferencingAttribute":"primarycontactid","ReferencedEntity":"contact","MetadataId":"410707b1-9554-4cd9-8437-6608b1802904"},{"ReferencingAttribute":"masterid","ReferencedEntity":"account","MetadataId":"51fa4af7-93d0-4f06-8949-38a0036ddc64"},{"ReferencingAttribute":"preferredsystemuserid","ReferencedEntity":"systemuser","MetadataId":"a6b48e23-fada-4b7f-8655-530bba050765"},{"ReferencingAttribute":"createdbyexternalparty","ReferencedEntity":"externalparty","MetadataId":"9967fe7d-84ee-4a26-9ad7-a8fdbdfa2316"},{"ReferencingAttribute":"modifiedby","ReferencedEntity":"systemuser","MetadataId":"8be02a9d-0776-4c76-b35f-1c92dd791d9e"},{"ReferencingAttribute":"parentaccountid","ReferencedEntity":"account","MetadataId":"57511732-b553-4cfb-bcf2-d280f9f8c6f1"},{"ReferencingAttribute":"entityimageid","ReferencedEntity":"imagedescriptor","MetadataId":"5b4942d5-1fcd-49ca-91c0-2737f5f104f3"},

此外,作为参考,我尝试对属性和目标执行 $expand
../api/data/v9.1/EntityDefinitions(LogicalName='account')?$select=LogicalName&$expand=Attributes($filter=AttributeType+eq+Microsoft.Dynamics.CRM.AttributeTypeCode%27Lookup%27&$expand=Targets)

它抛出一个错误:

"Query option '$expand' was specified more than once, but it must be specified at most once."

如果用逗号分隔,则可以在同一调用中展开属性和 ManyToOneRelationships。

GET https://{{baseUrl}}/api/data/v9.1/EntityDefinitions(LogicalName='account')?$select=LogicalName,EntitySetName&$expand=Attributes($select=LogicalName),ManyToOneRelationships($select=ReferencingAttribute,ReferencedEntity)