若要设置要返回的数据行的条件,请使用实体、链接实体或其他元素中的filter。
若要设置条件,请将一个或多个 条件元素 添加到筛选器。 包含 filtertype 属性确定是否需要满足条件中的 所有 (and)或 任何 (or)。 默认值为 and。 通过嵌套筛选元素,您可以创建结合 and 或 or 评估条件的复杂筛选条件。
每个 condition 都有一个 operator 属性用于评估行列值。 可以从许多 条件运算符值中进行选择。
例如,以下查询返回等于“Redmond”的 address1_city 帐户记录。 它与<filter type='and'>一起使用eq 运算符。
<fetch>
<entity name='account'>
<attribute name='name' />
<filter type='and'>
<condition attribute='address1_city'
operator='eq'
value='Redmond' />
</filter>
</entity>
</fetch>
此查询返回等于“Redmond”、“Seattle”或“Bellevue”的 address1_city 帐户记录。 它使用 <filter type='or'>,与三个 条件元素 一起,每个元素都使用 eq 运算符。
<fetch>
<entity name='account'>
<attribute name='name' />
<attribute name='address1_city' />
<filter type='or'>
<condition attribute='address1_city'
operator='eq'
value='Redmond' />
<condition attribute='address1_city'
operator='eq'
value='Seattle' />
<condition attribute='address1_city'
operator='eq'
value='Bellevue' />
</filter>
</entity>
</fetch>
还可以将 in 运算符 与单个 condition 元素一起使用来表示上一个查询。 此条件包含多个 值元素 ,用于指定要进行比较 address1_city的值。
<fetch>
<entity name='account'>
<attribute name='name' />
<attribute name='address1_city' />
<filter type='and'>
<condition attribute='address1_city'
operator='in'>
<value>Redmond</value>
<value>Seattle</value>
<value>Bellevue</value>
</condition>
</filter>
</entity>
</fetch>
运算符参数
运算符不需要任何参数、单个参数或多个参数。 运算符确定如何设置要计算的值。
无参数
某些运算符不需要任何参数。 例如,使用 eq-userid 运算符 来评估任何唯一标识符,并确定它是否与调用用户的 ID 匹配。
<condition attribute='ownerid'
operator='eq-userid' />
单个参数
当运算符需要单个参数时,请使用 value 属性来设置要计算的值。
例如,使用 eq 运算符通过设置statecode属性来评估value记录的选择列值。
<condition attribute='statecode'
operator='eq'
value='0' />
多个参数
当运算符需要多个参数时,请使用 value 元素 指定要计算的值。 例如,使用 between 运算符 计算一个数字,以确定它是否在一组值之间。
<condition attribute="numberofemployees"
operator="between">
<value>6</value>
<value>20</value>
</condition>
链接实体的筛选
当您在链接实体中应用筛选器时,除非将筛选器配置为在联接之后发生,否则筛选器将与联接一起应用。
当 链接实体link-type 的属性值为 outer时,您可能需要通过设置 条件entityname 的属性值,在链接之后应用过滤器。 如果您使用的是链接实体alias,请使用 alias 值来设置 entityname 属性。 否则,请将 entityname 属性值设置为 链接实体name 属性值。
例如,以下查询返回没有 父帐户的联系人,或者返回没有 传真的父帐户。
<fetch>
<entity name='contact'>
<attribute name='fullname' />
<filter>
<condition entityname='a'
attribute='fax'
operator='null' />
</filter>
<link-entity name='account'
from='accountid'
to='parentcustomerid'
link-type='outer'
alias='a' />
</entity>
</fetch>
按同一行中的列值筛选
可以使用 valueof 属性创建筛选器,对比同一行中各列的值。 例如,如果要查找firstname列值与lastname列值匹配的联系人记录,请使用以下查询:
<fetch>
<entity name='contact' >
<attribute name='firstname' />
<filter>
<condition attribute='firstname'
operator='eq'
valueof='lastname' />
</filter>
</entity>
</fetch>
跨表格列比较
仅使用 FetchXML,可以比较相关表中的字段值。 以下示例返回联系人 fullname 列与帐户 name 列匹配的行。
<fetch>
<entity name='contact'>
<attribute name='contactid' />
<attribute name='fullname' />
<filter type='and'>
<condition attribute='fullname'
operator='eq'
valueof='acct.name' />
</filter>
<link-entity name='account'
from='accountid'
to='parentcustomerid'
link-type='outer'
alias='acct'>
<attribute name='name' />
</link-entity>
</entity>
</fetch>
链接实体元素必须使用属性alias,参数的值valueof必须引用该别名和相关表中的列名。
列比较筛选器的限制
这些类型的筛选器具有以下限制:
-
Operator 说明 eq 将这些值进行相等性比较。 ne 这两个值不相等。 gt 该值大于比较值。 ge 该值大于或等于比较值。 lt 该值小于比较值。 le 该值小于或等于比较值。 一次只能比较两列。
不支持扩展条件操作。 例如:
valueof='amount'+ 100。列的类型必须相同。 例如:不能将字符串值与数字值进行比较。
筛选相关记录中的值
若要在不返回这些值的情况下筛选相关记录中的值,请使用筛选器元素中的链接实体元素和下列link-type属性之一:
| 名称 | 说明 |
|---|---|
any |
在 筛选器元素中使用此属性。 将结果限制为在链接实体中存在任何匹配行的父行。 |
not any |
在 筛选器元素中使用此属性。 将结果限制为在链接实体中不存在匹配行的父行。 |
all |
在 筛选器元素中使用此属性。 将结果限制为父行,且在链接实体中存在具有匹配 from 列值的行,但这些匹配行 均不满足为此链接实体定义的附加筛选条件。 您需要反转附加筛选条件,以查找父行,其中所有匹配的链接实体行均满足某些附加条件。 |
not all |
在 筛选器元素中使用此属性。 将结果限制为在链接实体中存在任何匹配行的父行。 此链接类型与any等效,尽管名称不同。 |
当您在过滤器元素中使用这些链接类型时,这些过滤器作为子条件,遵循父级 filter 的 type 属性定义的行为。
使用这些类型的筛选器最多返回一次父行,即使链接实体中存在多个匹配行。 它们不会从链接实体行中返回列值。
相关记录中值的筛选器示例
以下示例演示如何筛选相关记录的值。 这些示例包括等效的 SQL 语句,以帮助解释行为。
或筛选 link-typeany
此查询使用类型为 or 的 filter,并包含类型为 any 的子 link-entity,以返回满足以下条件的联系人记录:
-
满足以下任一条件:被至少一个 account 记录的 PrimaryContactId 查找列引用,且该记录的 Name 列等于
Contoso, -
或者 将 Contact.StateCode 选取列表列 设置为
1: 非活动:
<fetch>
<entity name='contact'>
<attribute name='fullname' />
<filter type='or'>
<link-entity name='account'
from='primarycontactid'
to='contactid'
link-type='any'>
<filter type='and'>
<condition attribute='name'
operator='eq'
value='Contoso' />
</filter>
</link-entity>
<condition attribute='statecode'
operator='eq'
value='1' />
</filter>
</entity>
</fetch>
link-type
not any
此查询使用 not any 链接类型,从 contact 表中返回未被PrimaryContactId 查找列,且其 Name 列等于 Contoso 的任何 account 记录所引用。 该 contact 记录仍可能被 account 记录引用,这些记录具有 otherName 列值。
<fetch>
<entity name='contact'>
<attribute name='fullname' />
<filter type='and'>
<link-entity name='account'
from='primarycontactid'
to='contactid'
link-type='not any'>
<filter type='and'>
<condition attribute='name'
operator='eq'
value='Contoso' />
</filter>
</link-entity>
</filter>
</entity>
</fetch>
link-type
not all
注释
all和not all链接类型的含义与名称可能暗示的相反,它们通常与倒排筛选器一起使用:
- 类型为
not all的链接实体等效于any,并返回那些具有与筛选条件匹配的链接实体记录的父记录。 - 当存在某些链接实体记录且其
from列值匹配,但这些链接实体行中没有满足 link-entity 元素内部定义的附加筛选条件时,类型为all的链接实体将返回父记录。
此查询使用类型为 not all 的 link-entity 来返回来自 contact 表的记录,这些记录被至少一个 account 记录的 PrimaryContactId 查找列所引用,且该记录的 Name 列等于 Contoso:
<fetch>
<entity name='contact'>
<attribute name='fullname' />
<filter type='and'>
<link-entity name='account'
from='primarycontactid'
to='contactid'
link-type='not all'>
<filter type='and'>
<condition attribute='name'
operator='eq'
value='Contoso' />
</filter>
</link-entity>
</filter>
</entity>
</fetch>
link-type
all
此查询使用类型为 all 的 link-entity 来返回来自 contact 表的记录,这些记录 被PrimaryContactId 查找列所引用的 某些account 记录,但没有一个 account 记录的 Name 列 等于 Contoso:
<fetch>
<entity name='contact'>
<attribute name='fullname' />
<filter type='and'>
<link-entity name='account'
from='primarycontactid'
to='contactid'
link-type='all'>
<filter type='and'>
<condition attribute='name'
operator='eq'
value='Contoso' />
</filter>
</link-entity>
</filter>
</entity>
</fetch>
条件限制
在 FetchXML 查询中,最多可以包含 500 个条件和链接实体元素。 否则,会看到以下错误消息:
名称:
TooManyConditionsInQuery
代码:0x8004430C
编号:-2147204340
消息:Number of conditions in query exceeded maximum limit.
需要减少执行查询的条件数。 可以使用可以与数字、唯一标识符和最多 850 个字符的字符串一起使用的 in 运算符 来减少条件数。
后续步骤
了解如何对结果进行分页。