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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

LinkEntities in C# add many relationships

(0) ShareShare
ReportReport
Posted on by 225

Hi i want to create a QueryExpression similar to this.

select
a.a,
a.b,
b.a,
c.a,
c.b

from a

inner join b on a.ida=b.ida
inner join c on b.idb=c.idb

My problema is the addlink how to manage this, b entity to c entity, when i make the addlink is a entity to b entity or u can make a enitity to c entity this way i can manage but no b to c.

Thanks in advance,

JP

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    ScottDurow Profile Picture
    21 on at

    Hi JP,

    The LinkEntity allows adding further sub links so you can join from b to c (msdn.microsoft.com/.../microsoft.xrm.sdk.query.linkentity.addlink.aspx)

    In fetch xml it would be:

    <fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true">

     <entity name="a">

       <attribute name="a" />

       <attribute name="b" />

       <link-entity name="b" from="bid" to="bid">

         <attribute name="a" /

         <link-entity name="c" from="cid" to="cid">

    <attribute name="a" />

        <attribute name="b" />  

         </link-entity>

       </link-entity>

     </entity>

    </fetch>

    So when you add the link from a to b, add another LinkEntity using AddLink from b to c.

    Hope this helps,

    Scott

  • 00f137054c6340ccb3ea9447e34ad682 Profile Picture
    225 on at

    Thanks Scott,

    Indeed that is the way. my two cents. fully working.

    QueryExpression SrchQry = new QueryExpression();

               SrchQry.EntityName = A.LogicalName;

               SrchQry.ColumnSet = new ColumnSet("a", "b", "c");

               SrchQry.Criteria.AddCondition("country", ConditionOperator.Equal, new string[] { strcodigo });

    LinkEntity EntityB = new LinkEntity(A.LogicalName, B.LogicalName, "aid", "aid", JoinOperator.Inner);

               EntityB.Columns = new ColumnSet("a", "b");

               EntityB.EntityAlias = "Entb";

    LinkEntity EntityC = new LinkEntity(B.LogicalName, C.LogicalName, "bid", "bid", JoinOperator.Inner);

               EntityC.Columns = new ColumnSet("a", "b");

               EntityC.EntityAlias = "Entc";

    SrchQry.LinkEntities.Add(EntityB);

    EntityB.LinkEntities.Add(EntityC);

    //Retrive

    EntityCollection RespuestaBusquedaQry = _service.RetrieveMultiple(SrchQry);

    Greetings,

    JP

  • Community Member Profile Picture
    on at

    hi juan... i wanna ask what is the "aid" there ?

    LinkEntity EntityB = new LinkEntity(A.LogicalName, B.LogicalName, "aid", "aid", JoinOperator.Inner);

    from what i know, when we want to use linkentity. There are four parameter we need to include inside the linkentity. there are linkfromentityname, linktoentityname, linkfromattributename and linktoattributename.

    iam able to understand the first two parameter. but i still did'nt get it for the last two parameter. Are linkfromattribute is primary key from the child entity and linktoattribute is foreign key in parent entity.

    can you give me simple explanation ?

  • 00f137054c6340ccb3ea9447e34ad682 Profile Picture
    225 on at

    Hi Saraswati,

    I use five parameters because u can put the type of join (inner, left ...), and yes is the queryexpresion(SrchQry) to linktoentity, that would be like this, new LinkEntity(SrchQry.LogicalName, B.LogicalName, "SrchQrylogicalid", "blogicalid", JoinOperator.Inner);

    Happy coding, greetings.

    JP

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans