다음을 통해 공유


샘플: QueryExpression 클래스를 사용하여 여러 항목 검색

 

게시 날짜: 2016년 11월

적용 대상: Dynamics CRM 2015

요구 사항

이 SDK에서 제공된 샘플 코드를 실행하기 위한 요구 사항에 대한 자세한 내용은 샘플 및 도우미 코드 사용을 참조하십시오.

예제

이 샘플에서는 관련 엔터티 열과 함께 QueryExpressionRetrieveMultiple 메서드를 사용하여 여러 엔터티를 검색하는 방법을 보여 줍니다. 코드는 기본 거래처 레코드에서 열을 반환할 뿐만 아니라 거래처에 연결된 기본 연락처의 성과 이름을 반환합니다.

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 사이 쿼리 변환

© 2017 Microsoft. All rights reserved. 저작권 정보