Sample: Retrieve multiple with the QueryExpression class

Applies to Dynamics 365 (online), version 9.x

Prerequisites

Internet connection is required to download the sample project and to restore the NuGet packages used in the sample project.

Example

This sample shows how to retrieve multiple entities using the IOrganizationService.RetrieveMultiple(QueryBase) method with QueryExpression along with their related entity columns. The code returns columns from the primary account record as well as the firstname and lastname of the primary contacts associated with the account.

To access aliased values, use the Entity.GetAttributeValue<T>(String) to get the AliasedValue.Value.

static void RetrieveMultipleWithRelatedEntityColumns(IOrganizationService service)
{
    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 = service.Create(contact);

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

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

    //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 = service.RetrieveMultiple(qe);

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

The code will output the following:

Entering:RetrieveMultipleWithRelatedEntityColumns
Retrieved 3 entities
account name: Test Account1
primary contact first name: ContactFirstName
primary contact first name: ContactLastName
account name: Test Account2
primary contact first name: ContactFirstName
primary contact first name: ContactLastName
account name: Test Account3
primary contact first name: ContactFirstName
primary contact first name: ContactLastName

See also

Use the QueryExpression Class
IOrganizationService
RetrieveMultiple(QueryBase)
QueryExpression
Build Queries with QueryExpression
Sample: Convert queries between Fetch and QueryExpression
QueryExpression