联接与 实体 或 链接实体 相关的表,以返回包含结果的其他列。 还与 筛选器 元素一起使用,以 对相关表中的列值应用条件。
例子
以下示例演示如何与不同类型的关系一起使用 link-entity 。
多对一关系
此查询基于帐户记录中的 PrimaryContactId 查找列从帐户表和联系人表返回数据:
<fetch>
<entity name='account'>
<attribute name='name' />
<link-entity name='contact'
from='contactid'
to='primarycontactid'
link-type='inner'
alias='contact'>
<attribute name='fullname' />
</link-entity>
</entity>
</fetch>
一对多关系
此查询基于联系人account_primary_contact一对多关系返回联系人表和帐户表的数据。
<fetch>
<entity name='contact'>
<attribute name='fullname' />
<link-entity name='account'
from='primarycontactid'
to='contactid'
alias='account'>
<attribute name='name' />
</link-entity>
</entity>
</fetch>
多对多关系
此查询使用teammembership_association多对多关系从 SystemUser 和 Team 表返回数据。
<fetch>
<entity name='systemuser'>
<attribute name='fullname' />
<link-entity name='teammembership'
from='systemuserid'
to='systemuserid' >
<link-entity name='team'
from='teamid'
to='teamid'
link-type='inner'
alias='team'>
<attribute name='name' />
</link-entity>
</link-entity>
</entity>
</fetch>
特性
| Name | Required? | Description |
|---|---|---|
name |
是的 | 关联表的逻辑名称。 |
to |
否 | 父 元素中 要与属性中指定的相关表列匹配的列的 from 逻辑名称。
虽然在技术上不需要,但通常使用此属性。 |
from |
否 | 与属性 中指定的列匹配 的相关表中的列的 to 逻辑名称。
虽然在技术上不需要,但通常使用此属性。 |
alias |
否 | 表示相关表的名称。 如果未设置别名,将生成一个别名,以确保所有列都具有唯一的名称,但无法使用该别名引用提取 XML 其他部分中的链接实体。 自动生成的别名使用模式 {LogicalName}+{N},其中 N 从 1 开始,提取 XML 中的链接实体的序列号。 |
link-type |
否 | 链接使用的类型。 默认行为是 内部行为。 了解链接类型选项 |
intersect |
否 | 指示 link-entity 用于联接表而不返回任何列,通常用于多对多关系。 此属性的存在不会更改查询执行。 联接表时,可以向你 link-entity 添加此属性,但不包含任何 属性元素 ,以显示这是有意的。 |
Using from 和 to attributes
最好同时设置 from 和 to 属性的值。 这两个属性通常用于显式定义要匹配的列。 但是, from 在技术上不需要属性 to 。
注释
重要的是,在属性中指定的
fromto列是相同的类型。 不支持使用不同的列类型。 当列的类型不相同时,Dataverse 基础结构可能能够强制转换,但这种做法可能会导致显著的性能损失。FetchXml 中属性的含义
fromto与使用 QueryExpression 撰写查询时使用的相应 LinkEntity.LinkFromAttributeName 和 LinkEntity.LinkToAttributeName 属性相反。
如果不使用这些属性之一,并且两个表之间存在系统多对多关系,则 Dataverse 会使用该关系选择适当的键值。
如果只指定其中一个 from 或 to 属性,Dataverse 会尝试使用两个表之间的关系架构定义找出正确的关系。
否则会出现以下错误:
代码:
0x80041102
消息:No system many-to-many relationship exists between <table A> and <table B>. If attempting to link through a custom many-to-many relationship ensure that you provide the from and to attributes.
例如,这两个查询都使用 systemuser 和团队表之间的teammembership_association多对多关系。 在这种情况下,Dataverse 可以处理 from 和 to 属性,并且 link-entity 不需要指定相交表。
指定所有属性
<fetch top="2">
<entity name="systemuser">
<attribute name="fullname" />
<link-entity
name="teammembership"
from="systemuserid"
to="systemuserid"
intersect="true"
>
<link-entity
name="team"
from="teamid"
to="teamid"
link-type="inner"
alias="team"
>
<attribute name="name" />
</link-entity>
</link-entity>
</entity>
</fetch>
让 Dataverse 选择
<fetch top="2">
<entity name="systemuser">
<attribute name="fullname" />
<link-entity name="team" alias="team">
<attribute name="name" />
</link-entity>
</entity>
</fetch>
链接类型选项
用于 link-type 对返回的记录应用筛选器。 下表描述了有效 link-type 值:
| Name | Description |
|---|---|
inner |
违约。 将结果限制为两个表中具有匹配值的行。 |
outer |
包括没有匹配值的父元素的结果。 |
any |
在 筛选器元素中使用此属性。 将结果限制为具有链接实体中任何匹配行的父行。
了解如何用于 any 筛选相关表上的值 |
not any |
在 筛选器元素中使用此属性。 将结果限制为没有链接实体中匹配行的父行。
了解如何用于 not any 筛选相关表上的值 |
all |
在 筛选器元素中使用此属性。 将结果限制为链接实体中存在具有匹配 from 列值的行的父行,但这些 匹配行都 不符合为此链接实体定义的其他筛选器。 需要 反转 其他筛选器来查找 每个 匹配链接实体行满足一些其他条件的父行。
了解如何用于 all 筛选相关表上的值 |
not all |
在 筛选器元素中使用此属性。 将结果限制为具有链接实体中任何匹配行的父行。 此链接类型与 any 名称相同。
了解如何用于 not all 筛选相关表上的值 |
exists |
其变体 inner 可提供性能优势。 在where子句中使用 EXISTS 条件。 当结果中不需要父行的多个副本时,请使用此方法。
详细了解是否存在 |
in |
其变体 inner 可提供性能优势。 在子句中使用 whereIN 条件。 当结果中不需要父行的多个副本时,请使用此方法。
详细了解是否存在 |
matchfirstrowusingcrossapply |
其变体 inner 可提供性能优势。 如果只有链接实体中匹配行的单个示例足够,并且结果中父行的多个副本不需要,则使用此类型。
详细了解 matchfirstrowusingcrossapply |
父元素
| Name | Description |
|---|---|
| 实体 | 指定 提取元素的子元素,即查询的“父实体”。 只允许一个实体。 |
| link-entity | 联接与 实体 或 链接实体 相关的表,以返回包含结果的更多列。 |
子元素
| Name | 事件 | Description |
|---|---|---|
| all-attributes | 0 或 1 | 指示返回每行的所有非 null 列值。 它与不添加任何 属性元素相同。 对于大多数情况,我们不建议使用此元素。 |
| 属性 | 0 或多 | 指定要使用查询返回的 实体 或 链接实体 中的列。 |
| order | 0 或多 | 指定结果行的排序顺序。 |
| link-entity | 0 或多 | 联接与 实体 或 链接实体 相关的表,以返回包含结果的更多列。 |
| 滤波器 | 0 或 1 | 为要应用于查询的 实体 或 链接实体 指定复杂条件。 |