サンプル: QueryExpression クラスを使用した複数取得

 

公開日: 2017年1月

対象: Dynamics 365 (online)、Dynamics 365 (on-premises)、Dynamics CRM 2016、Dynamics CRM Online

要件

この SDK で提供するサンプル コードを実行するために必要な要件については、「サンプルとヘルパー コードの使用」を参照してください。

このサンプルでは、RetrieveMultiple メソッドを QueryExpression とともに使用して、複数のエンティティを関連エンティティ列と共に取得する方法について示しています。 コードでは、プライマリ取引先企業レコードの列と、アカウントに関連付けられている取引先責任者の姓名が返されます。

static void RetrieveMultipleWithRelatedEntityColumns()
{
    Console.WriteLine("Entering:RetrieveMultipleWithRelatedEntityColumns");
    //Create multiple accounts with primary contacts
    Entity contact = new Entity("contact");
    contact.Attributes["firstname"] = "ContactFirstName";
    contact.Attributes["lastname"] = "ContactLastName";
    Guid contactId = _orgService.Create(contact, null);

    Entity account = new Entity("account");
    account["name"] = "Test Account1";
    EntityReference primaryContactId = new EntityReference("contact", contactId);
    account["primarycontactid"] = primaryContactId;

    Guid accountId1 = _orgService.Create(account, null);
    account["name"] = "Test Account2";
    Guid accountId2 = _orgService.Create(account, null);
    account["name"] = "Test Account3";
    Guid accountId3 = _orgService.Create(account, null);

    //Create a query expression specifying the link entity alias and the columns of the link entity that you want to return
    QueryExpression qe = new QueryExpression();
    qe.EntityName = "account";
    qe.ColumnSet = new ColumnSet();
    qe.ColumnSet.Columns.Add("name");

    qe.LinkEntities.Add(new LinkEntity("account", "contact", "primarycontactid", "contactid", JoinOperator.Inner));
    qe.LinkEntities[0].Columns.AddColumns("firstname", "lastname");
    qe.LinkEntities[0].EntityAlias = "primarycontact";

    EntityCollection ec = _orgService.RetrieveMultiple(qe);

    Console.WriteLine("Retrieved {0} entities", ec.Entities.Count);
    foreach (Entity act in ec.Entities)
    {
       Console.WriteLine("account name:" + act["name"]);
       Console.WriteLine("primary contact first name:" + act["primarycontact.firstname"]);
       Console.WriteLine("primary contact last name:" + act["primarycontact.lastname"]);
    }
}

関連項目

IOrganizationService
RetrieveMultiple
QueryExpression
QueryExpression
QueryExpression クラスの使用
QueryExpression でクエリを作成する
サンプル: Fetch と QueryExpression の間でクエリを変換する

Microsoft Dynamics 365

© 2017 Microsoft. All rights reserved. 著作権