範例:使用 QueryExpression 類別取得多個
發行︰ 2017年1月
適用於: Dynamics 365 (online)、Dynamics 365 (on-premises)、Dynamics CRM 2016、Dynamics CRM Online
需求
如需執行此 SDK 所提供範例程式碼的需求資訊,請參閱使用範例和 Helper 程式碼。
範例
此範例顯示如何使用 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. 著作權所有,並保留一切權利。 著作權