示例:使用一对多关系进行检索

 

发布日期: 2017年1月

适用于: Dynamics 365 (online),Dynamics 365 (on-premises),Dynamics CRM 2016,Dynamics CRM Online

要求

有关运行此 SDK 中提供的示例代码的要求的详细信息,请参阅使用示例和帮助程序代码

示例

以下示例显示如何使用 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。 保留所有权利。 版权