在单个查询中检索所有记录

时间:2019-04-01 05:08:04

标签: c# xml-parsing dynamics-crm

我已经创建(1)2个帐户(第一个将是第二个帐户的父级) (2)创建2个联系人(关联的第一个联系人将是第一个帐户,第二个联系人将与第二个帐户) (3)创建一个便笺并将其与上级帐户关联 (4)创建了两个便笺并将其与第二联系人相关联

现在我想在单个查询中检索所有记录。

我尝试了但没有得到结果。

var fetch = "<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' link-type='inner' alias='bd'>" +
                    "<attribute name='fullname' />" +
                      "<link-entity name='annotation' from='objectid' to='contactid' link-type='inner' alias='be'>" +
                        "<filter type='and'>" +
                          "<condition attribute='subject' operator='not-null' />" +
                        "</filter>" +
                      "</link-entity>" +
                    "</link-entity>" +
                  "</entity>" +
                "</fetch>";

 EntityCollection Coll = CrmConn.RetrieveMultiple(new FetchExpression(fetch));

                foreach (Entity acunt in Coll.Entities)
                {
                    Console.WriteLine("Name of Account: " + acunt.GetAttributeValue<string>("name"));
                    Console.WriteLine("Name of Contact: "  +acunt.GetAttributeValue<string>("fullname"));
                    Console.WriteLine("Name of Notes: " + acunt.GetAttributeValue<string>("subject"));
                    //Console.WriteLine("contact: ", Coll.Entities[]);
                 }

仅显示帐户名称,不显示备注和联系人

1 个答案:

答案 0 :(得分:0)

确定,使用以下查询仅检索客户及其相关联系人。我可以检索帐户及其相关联系人。 现在获取这些帐户及其ID,并使用第二个查询来获取注释(注释) 与Notes相同。

<fetch>
  <entity name="account" >
    <attribute name="name" />
    <link-entity name="contact" from="parentcustomerid" to="accountid" link-type="inner" >
      <attribute name="fullname" />
    </link-entity>
  </entity>
</fetch>

查询回溯笔记:

<fetch>
  <entity name="annotation" >
    <attribute name="filename" />
    <attribute name="documentbody" />
    <attribute name="isdocument" />
    <link-entity name="account" from="accountid" to="objectid" link-type="inner" >
      <filter type="and" >
        <condition attribute="accountid" operator="eq" value="AccountGuid" />
      </filter>
    </link-entity>
  </entity>
</fetch>

带有C#代码的示例

string fetch = @"  
   <fetch>
  <entity name='account' >
    <attribute name='name' />
    <link-entity name='contact' from='parentcustomerid' to='accountid' link-type='inner' alias='Contact' >
      <attribute name='fullname' alias = 'Contact.Fullname' />
    </link-entity>
  </entity>
</fetch> ";

           EntityCollection Coll = CrmConn.RetrieveMultiple(new FetchExpression(fetch));

                foreach (Entity acunt in Coll.Entities)
                {
                    Console.WriteLine("Name of Account: " + acunt.GetAttributeValue<string>("name"));
                    Console.WriteLine("Name of Contact: "  + acunt.GetAttributeValue<AliasedValue>("Contact.Fullname").Value);
                 }