共用方式為


範例:擷取與一對多關係

 

發行︰ 2017年1月

適用於: Dynamics 365 (online)、Dynamics 365 (on-premises)、Dynamics CRM 2016、Dynamics CRM Online

需求

如需執行此 SDK 所提供範例程式碼的需求資訊,請參閱使用範例和 Helper 程式碼

範例

下列範例顯示如何使用 RetrieveMultiple 方法同時傳回實體的 1:N 相關實體和主要實體。 在此範例中,我們會擷取客戶及其相關工作。

Console.WriteLine("One-to-many Query using RetrieveMultiple");
    Console.WriteLine();
//Create an account and three related tasks
    Account account = new Account();
    account.Name = "SDK Test Account";
    Guid acctId = _serviceProxy.Create(account);
    Console.WriteLine("New account created.");
    Console.WriteLine();

    Task task1 = new Task();
    task1.Subject = "Test Task1";
    task1.RegardingObjectId = new EntityReference("account", acctId);
    Guid task1Id = _serviceProxy.Create(task1);

    Task task2 = new Task();
    task2.Subject = "Test Task2";
    task2.RegardingObjectId = new EntityReference("account", acctId);
    Guid task2Id = _serviceProxy.Create(task2);

    Task task3 = new Task();
    task3.Subject = "Test Task3";
    task3.RegardingObjectId = new EntityReference("account", acctId);
    Guid task3Id = _serviceProxy.Create(task2);

    Console.WriteLine("Three tasks created.");
    Console.WriteLine();

// Construct query
    // Condition where task attribute equals account id. 
    ConditionExpression condition = new ConditionExpression();
    condition.AttributeName = "regardingobjectid";
    condition.Operator = ConditionOperator.Equal;
    condition.Values.Add(acctId.ToString());

    //Create a column set.
    ColumnSet columns = new ColumnSet("subject");

    // Create query expression.
    QueryExpression query1 = new QueryExpression();
    query1.ColumnSet = columns;
    query1.EntityName = "task";
    query1.Criteria.AddCondition(condition);

    EntityCollection result1 = _serviceProxy.RetrieveMultiple(query1);

    foreach (var r in result1.Entities)
    {
        Console.WriteLine(r.Attributes["subject"]);
    };
    Console.WriteLine("-------------------------------");
    Console.WriteLine();

另請參閱

IOrganizationService
QueryExpression
IOrganizationService
RetrieveMultiple
使用 QueryExpression 建立查詢
範例:使用 QueryByAttribute 類別取得多個

Microsoft Dynamics 365

© 2017 Microsoft. 著作權所有,並保留一切權利。 著作權