使用 FetchXML 筛选行

若要设置要返回的数据行的条件,请使用实体链接实体或其他元素中的filter

若要设置条件,请将一个或多个 条件元素 添加到筛选器。 包含 filtertype 属性确定是否需要满足条件中的 所有and)或 任何or)。 默认值为 and。 通过嵌套筛选器元素,您可以创建复杂的筛选条件,以结合使用andor进行评估的条件。

每个 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必须引用该别名和相关表中的列名。

列比较筛选器的限制

这些类型的筛选器具有以下限制:

  • 条件 只能使用以下 运算符

    操作员 说明
    eq 将这些值进行相等性比较。
    ne 这两个值不相等。
    gt 该值大于比较值。
    ge 该值大于或等于比较值。
    lt 该值小于比较值。
    le 该值小于或等于比较值。
  • 一次只能比较两列。

  • 不支持扩展条件操作。 例如: valueof='amount'+ 100

  • 列的类型必须相同。 例如:不能将字符串值与数字值进行比较。

若要在不返回这些值的情况下筛选相关记录中的值,请使用筛选器元素中的链接实体元素和下列link-type属性之一:

名称 说明
any 筛选器元素中使用此属性。 将结果限制为具有链接实体中任何匹配行的父行。
not any 筛选器元素中使用此属性。 将结果限制为父行,这些父行在链接实体中没有匹配的行。
all 筛选器元素中使用此属性。 将结果限制为父行,这些父行中存在与链接实体中 from 列值匹配的行,但这些匹配行没有一个符合为此链接实体定义的其他筛选器。 需要将附加筛选器反转,以查找其中每个匹配的链接实体行都满足某些附加条件的父行。
not all 筛选器元素中使用此属性。 将结果限制为具有链接实体中任何匹配行的父行。 此链接类型与any等效,尽管名称不同。

筛选器元素内使用这些链接类型时,这些筛选器是遵循父type属性定义的filter行为的子条件。

使用这些类型的筛选器最多返回一次父行,即使链接实体中存在多个匹配行。 它们不会从链接实体行中返回列值。

以下示例演示如何筛选相关记录的值。 这些示例包括等效的 SQL 语句,以帮助解释行为。

此查询使用类型为filter和子类型为orlink-entity进行查询,以从any中返回记录,这些记录的类型为

<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>

此查询使用not any链接类型,从联系人表中返回那些未被任何帐户记录的PrimaryContactId 查找列引用的记录,其中这些帐户记录的Contoso等于。 联系人记录可能仍由具有其他Name 列值的帐户记录引用。

<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>

注释

allnot all链接类型的含义与名称可能暗示的相反,它们通常与倒排筛选器一起使用:

  • 类型为 not all 的链接实体等效于 any,并返回那些具有与筛选条件匹配的链接实体记录的父记录。
  • 如果存在具有匹配all列值的一些链接实体记录,但这些链接实体行from不符合链接实体元素中定义的附加筛选器,则类型链接实体将返回父记录。

此查询使用一种link-entity类型not all返回联系人表中的记录,这些记录由至少一个帐户记录的 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>

此查询使用类型,从联系人表中返回由某些帐户记录的PrimaryContactId 查找列引用的记录,但这些帐户记录中没有名称列等于的记录。

<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 运算符 来减少条件数。

后续步骤

了解如何对结果进行分页。