代码之家  ›  专栏  ›  技术社区  ›  paulwhit

可以编写一个fetchxml查询来获取1:many关系吗?

  •  2
  • paulwhit  · 技术社区  · 15 年前

    是否可以编写一个获取根实体和多个子项的fetchxml查询?我所能做的就是1:1。

    4 回复  |  直到 8 年前
        1
  •  5
  •   Matt    15 年前

    不,这是不可能的。

        2
  •  5
  •   Cody Gray    8 年前

    James Wood is correct . 提取XML是递归的,因此通过使用链接实体,您可以获得所需的信息。

    例如,以下内容是有效的:

    <fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true">
      <entity name="account">
        <attribute name="name" />
        <attribute name="primarycontactid" />
        <attribute name="telephone1" />
        <attribute name="accountid" />
        <order attribute="name" descending="false" />
        <link-entity name="contact" from="parentcustomerid" to="accountid" alias="aj">
            <attribute name="firstname" />
            <attribute name="lastname" />
            <attribute name="telephone1" />
            <link-entity name="businessunit" from="businessunitid" to="owningbusinessunit" alias="ak">
                <attribute name="name" />
                <attribute name="address1_line1" />
                <attribute name="address1_line2" />
                <attribute name="address1_line3" />
                <filter type="and">
                  <condition attribute="name" operator="not-null" />
                </filter>
            </link-entity>
        </link-entity>
      </entity>
    </fetch>
    

    如果您的问题真的是“是否可以编写一个fetchxml查询来获取 单一的 很遗憾,答案是否定的。但是,如果您能够处理根数据的重复项(例如使用SSRS报告的分组功能),那么您所需要的是完全可能的。

        3
  •  4
  •   James Wood    12 年前

    除非我误解了这个问题,否则这是非常可能的。

    例如,您希望查找与给定帐户相关的所有联系人。这在CRM中由客户联系人的上级客户查找来表示。

    <fetch mapping=“logical”count=“100”version=“1.0”>
    <entity name=“account”>
    <attribute name=“name”/>
    <link entity name=“contact”from=“parentcustomerid”to=“accountid”>
    <attribute name=“fullname”/>
    </Link实体>
    & /实体& GT;
    &;
    < /代码> 
    
    

    它会给您一个如下的结果集:

     
    示例:要查找与给定帐户相关的所有联系人。这在CRM中由客户的联系人的上级客户查找表示。

    enter image description here

    <fetch mapping="logical" count="100" version="1.0">
        <entity name="account">
            <attribute name="name" />
            <link-entity name="contact" from="parentcustomerid" to="accountid">
                <attribute name="fullname" />
            </link-entity>
        </entity>
    </fetch>
    

    这将为您提供一个如下所示的结果集:

    <resultset morerecords="0" paging-cookie="&lt;cookie page=&quot;1&quot;&gt;&lt;accountid last=&quot;{E704FAD6-2D4B-E111-9FED-00155D828444}&quot; first=&quot;{AD912122-6B3C-E111-9B37-00155D828444}&quot; /&gt;&lt;/cookie&gt;">
        <result>
            <name>RGD Mining Inc</name>
            <accountid>{E704FAD6-2D4B-E111-9FED-00155D828444}</accountid>
            <accountid.fullname>Bill Miner</accountid.fullname>
        </result>
        <result>
            <name>RGD Mining Inc</name>
            <accountid>{E704FAD6-2D4B-E111-9FED-00155D828444}</accountid>
            <accountid.fullname>Green</accountid.fullname>
        </result>
    </resultset>
    
        4
  •  0
  •   Cody Gray    8 年前

    我很高兴地报告说这是可能的。我有一个很好的解决方案。

    唯一需要注意的是,如果您有多个子帐户,您将为父帐户获得多个结果。例如:

    parent 1: child 1
    parent 2: child 1
    parent 2: child 2
    

    这意味着您必须通过排序函数来运行结果,以便在多维数组中获取父项下的所有子项,或者将所有帐户作为平面数组中的唯一条目来获取。

    而且,这只会下降一个级别。如果层次结构是多级的,则需要修改此代码。

    <fetch distinct="false" mapping="logical"> 
       <entity name="account">     
            <attribute name="name" />
            <link-entity name="account"  alias="childaccount" to="accountid" from="parentaccountid"  link-type="outer">
                <attribute name="name" alias="childname" />
            </link-entity>
        </entity>           
    </fetch>
    
    推荐文章