示例:使用 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。 保留所有权利。 版权