You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否合并获取相同数据但链接实体过滤器不同的两个FetchXML?

如何合并两个FetchXML查询?

可以合并这两个FetchXML查询,通过将各自的专属过滤条件用顶级OR逻辑组包裹,同时保留所有通用条件,就能实现逻辑等价的单查询,减少数据库请求次数以优化性能。

合并核心逻辑

  • 所有标记为//Common的条件保留在全局逻辑中,作为两个查询的共同判定基础
  • 两个查询的//Unique条件分别封装到独立的AND子过滤器中,再用顶级OR过滤器包裹,实现「满足查询1专属条件 OR 满足查询2专属条件」的逻辑

合并后的FetchXML代码

<fetch>
  <entity name="msdyn_workorder" >
    <filter type="or" >
      <!-- 查询1的专属条件 + 通用条件 -->
      <filter type="and" >
        <!-- 主实体通用条件 -->
        <condition attribute="statecode" operator="neq" value="1" /> <!-- Common -->
        <condition attribute="wo_regionname" operator="in" >@[$Project::IncludeRegion]</condition> <!-- Common -->
        
        <!-- 查询1专属链接实体过滤 -->
        <link-entity name="msdyn_workorderproduct" from="msdyn_workorder" to="msdyn_workorderid" link-type="inner" alias="wop" >
          <filter type="and" >
            <condition attribute="statecode" operator="neq" value="1" /> <!-- Common -->
            <condition attribute="tel_wo_unknownpart" operator="neq" value="1" /> <!-- Common -->
            <condition attribute="tel_wo_installedqtysubmitstatus" operator="eq" value="100000200" /> <!-- Unique -->
          </filter>
          <link-entity name="uom" from="uomid" to="msdyn_unit" link-type="outer" alias="uom" >
            <attribute name="name" />
          </link-entity>
          <link-entity name="tel_wo_partsubcategory" from="tel_wo_partsubcategoryid" to="tel_wo_subcategory" link-type="inner" alias="subcategory" >
            <filter type="or" >
              <condition attribute="tel_wo_code" operator="eq" value="001001" /> <!-- Common -->
              <condition attribute="tel_wo_code" operator="eq" value="002002" /> <!-- Common -->
            </filter>
            <link-entity name="tel_wo_partcategory" from="tel_wo_partcategoryid" to="tel_wo_partcategory" link-type="inner" alias="category" >
              <filter type="or" >
                <condition attribute="tel_wo_code" operator="eq" value="001" /> <!-- Common -->
                <condition attribute="tel_wo_code" operator="eq" value="002" /> <!-- Common -->
              </filter>
            </link-entity>
          </link-entity>
          <order attribute="msdyn_workorderproductid" />
        </link-entity>
        
        <link-entity name="territory" from="territoryid" to="wo_region" link-type="inner" alias="Region" >
          <filter>
            <condition attribute="tel_com_sendcs0130afterinstalledqtychanged" operator="eq" value="1" /> <!-- Unique -->
          </filter>
        </link-entity>
      </filter>

      <!-- 查询2的专属条件 + 通用条件 -->
      <filter type="and" >
        <!-- 主实体通用条件 -->
        <condition attribute="statecode" operator="neq" value="1" /> <!-- Common -->
        <condition attribute="wo_regionname" operator="in" >@[$Project::IncludeRegion]</condition> <!-- Common -->
        
        <!-- 查询2主实体专属条件 -->
        <condition attribute="msdyn_timeclosed" operator="not-null" /> <!-- Unique -->
        <condition attribute="tel_wo_trigger_installedparts_integration" operator="eq" value="1" /> <!-- Unique -->
        
        <!-- 查询2专属链接实体过滤 -->
        <link-entity name="msdyn_workorderproduct" from="msdyn_workorder" to="msdyn_workorderid" link-type="inner" alias="wop" >
          <filter type="and" >
            <condition attribute="statecode" operator="neq" value="1" /> <!-- Common -->
            <condition attribute="tel_wo_unknownpart" operator="neq" value="1" /> <!-- Common -->
          </filter>
          <link-entity name="uom" from="uomid" to="msdyn_unit" link-type="outer" alias="uom" >
            <attribute name="name" />
          </link-entity>
          <link-entity name="tel_wo_partsubcategory" from="tel_wo_partsubcategoryid" to="tel_wo_subcategory" link-type="inner" alias="subcategory" >
            <attribute name="tel_wo_code" />
            <filter type="or" >
              <condition attribute="tel_wo_code" operator="eq" value="001001" /> <!-- Common -->
              <condition attribute="tel_wo_code" operator="eq" value="001002" /> <!-- Common -->
              <condition attribute="tel_wo_code" operator="eq" value="002001" /> <!-- Unique -->
              <condition attribute="tel_wo_code" operator="eq" value="002002" /> <!-- Unique -->
              <condition attribute="tel_wo_code" operator="eq" value="002003" /> <!-- Unique -->
              <condition attribute="tel_wo_code" operator="eq" value="004004" /> <!-- Unique -->
            </filter>
            <link-entity name="tel_wo_partcategory" from="tel_wo_partcategoryid" to="tel_wo_partcategory" link-type="inner" alias="category" >
              <attribute name="tel_wo_code" />
              <filter type="or" >
                <condition attribute="tel_wo_code" operator="eq" value="001" /> <!-- Common -->
                <condition attribute="tel_wo_code" operator="eq" value="002" /> <!-- Common -->
                <condition attribute="tel_wo_code" operator="eq" value="004" /> <!-- Unique -->
              </filter>
            </link-entity>
          </link-entity>
          <order attribute="msdyn_workorderproductid" />
        </link-entity>
        
        <link-entity name="territory" from="territoryid" to="wo_region" link-type="inner" alias="Region" >
          <attribute name="tel_com_sendcs0130afterinstalledqtychanged" />
        </link-entity>
      </filter>
    </filter>

    <!-- 通用链接实体(两个查询无差异部分) -->
    <link-entity name="tel_com_salesorderheader" from="tel_com_salesorderheaderid" to="tel_wo_primary_so" link-type="outer" alias="primaryso" >
      <attribute name="tel_com_sales_document_no" />
    </link-entity>
  </entity>
</fetch>

注意事项

  • 保持所有链接实体的别名(如wop、Region)一致,避免结果字段映射混乱
  • 测试合并后的查询结果,验证是否与原两个查询的结果集完全一致,避免逻辑疏漏

内容的提问来源于stack exchange,提问作者gymcode

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 04:08:12