Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested answer

Retrieving parent of parent

(0) ShareShare
ReportReport
Posted on by 35

How to get Lookup of a parent using Linq Query..?

  • cloflyMao Profile Picture
    cloflyMao 25,202 on at
    RE: Retrieving parent of parent

    Hi mrutyunjai,

    Was code run successfully?

    Regards,

    Clofly

  • cloflyMao Profile Picture
    cloflyMao 25,202 on at
    RE: Retrieving parent of parent

    Hi mrutyunjai,

    Feel free to ask any question if you had encountered any problem when running the code.

    Regards,

    Clofly

  • mrutyunjai Profile Picture
    mrutyunjai 35 on at
    RE: Retrieving parent of parent

    Thank you Clofly, i will try this

  • Suggested answer
    cloflyMao Profile Picture
    cloflyMao 25,202 on at
    RE: Retrieving parent of parent

    Hi mrutyunjai,

    We can combine two "join on" to get parent record of parent record.

    For example, Lead > Contact(Existing Contact?) > Account(Company Name).

    Clofly Mao > Clofly Mao > Contoso, Ltd.

    8321.JPG

    pastedimage1603335374544v1.png

    public static void queryEntity(IOrganizationService organizationService)
    {
    
        OrganizationServiceContext orgContext = new OrganizationServiceContext(organizationService);
    
        var myLinqQuery = from leadEntity in orgContext.CreateQuery("lead")
    
                          join contactEntity in orgContext.CreateQuery("contact")
    
                          on leadEntity["parentcontactid"] equals contactEntity["contactid"]
    
                          join accountEntity in orgContext.CreateQuery("account")
    
                          on contactEntity["parentcustomerid"] equals accountEntity["accountid"]
    
                          select new
                          {
                              fullname = leadEntity.GetAttributeValue("fullname"),
                              contactname = contactEntity.GetAttributeValue("fullname"),
                              accountname = accountEntity.GetAttributeValue("name"),
                              accountphone = accountEntity.GetAttributeValue("telephone1"),
                          };
    
        StringBuilder builder = new StringBuilder();
    
        foreach (var results in myLinqQuery)
        {
            builder.Append("============================================"   "\n");
            builder.Append(" lead fullname: "   results.fullname   ", parent contact: "   results.contactname
                              ", parent account name of parent contact: "   results.accountname   ", parent account phone of parent contact: "   results.accountphone   "\n");
            builder.Append("============================================");
    
        }
    
        Console.WriteLine(builder.ToString());
    
    }

    Regards,

    Clofly

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,409 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans