web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to retrieve record using c# for N to N relationship

(0) ShareShare
ReportReport
Posted on by

Hello,

I've been looking at examples on the different websites/post but I still don't understand how to retrieve a record in N to N relationship.

Say I have Entity A and B they have N to N relationship.

There are two records in Entity A that is linked to a record Entity B.

How can I retrieve those 2 records from the record in Entity B using C#?

Some example code are really appreciated!

Thank you!

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Joshua Wright Profile Picture
    410 on at

    Advanced find is your friend. N:N relationships actually create a hidden table that can be seen if you build an advanced find query and download the fetch xml. This can also be seen from the N:N relationship, but I find the advanced find to be a much quick way to set up the FetchXml query for C# use. From there standard serviceProxy.RetrieveMultiple(new FetchExpression(strFetch)) call will return the records(EntityCollection) you are looking for. Below I have included a screenshot of an advanced find between our Security Access and Location entities (N:N relatioship)

    N_5F00_NAdvFind.PNG

    Downloaded Fetch

    <fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true">
      <entity name="new_buildingaccess">
        <attribute name="new_name" />
        <attribute name="createdon" />
        <attribute name="new_buildingaccessid" />
        <order attribute="new_name" descending="false" />
        <link-entity name="new_location_securityaccess" from="new_buildingaccessid" to="new_buildingaccessid" visible="false" intersect="true">
          <link-entity name="new_location" from="new_locationid" to="new_locationid" alias="ad">
            <filter type="and">
              <condition attribute="new_locationid" operator="eq" uiname="" uitype="new_location" value="" />
            </filter>
          </link-entity>
        </link-entity>
      </entity>
    </fetch>


  • Suggested answer
    Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    Hi Heimmu,

    Just to add to Joshua's response, the following is the way to use the fetch in C# code:

    string fetchXml =

    String.Format(@'<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>
    <entity name='new_buildingaccess'>

       <attribute name='new_name' />

       <attribute name='createdon' />

       <attribute name='new_buildingaccessid' />

       <order attribute='new_name' descending='false' />

       <link-entity name='new_location_securityaccess' from='new_buildingaccessid' to='new_buildingaccessid' visible='false' intersect='true'>

        <link-entity name='new_location' from='new_locationid' to='new_locationid' alias='ad'>

          <filter type='and'>

             <condition attribute='new_locationid' operator='eq' uiname='' uitype='new_location' value='{0}' />

          </filter>

        </link-entity>

    </link-entity>

    </entity>

    </fetch>', locationId.ToString());

    EntityCollection results = new EntityCollection();

    FetchExpression fe = new FetchExpression(fetchXml);

    try

    {

       EntityCollection results = service.RetrieveMultiple(fe);

       if (results.Entities.Count > 0) 

       {

          foreach (Entity result in results.Entities)

          {

             // Your Code here

          }

       }

    }

    catch (FaultException<OrganizationServiceFault> ex)

    {

       throw new InvalidPluginExecutionException('An error occurred in the RetrieveMultiple function', ex);

    }

    Hope this helps...

  • Verified answer
    Hemant Kumar Sahu Profile Picture
    1,829 on at

    Hi Heimmu,

    + to above answers,

    When you create N:N, it creates a table, the table will have the GuidFirstEntity and GuidSecondEntity as columns. So you can query on easily.

    You can query on the third entity(relationship table) by guid of Entity B, You may use QueryExpression/RetriveMultiple/FetchXml.

    Thanks

    Hemant

  • Community Member Profile Picture
    on at

    Thank you so much all!!! I really appreciate the help!!

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans