Damkulul
Damkulul

Reputation: 1476

How to retreive an entity according to multiple linkentities?

I retrieve an Entity according to filtering with another entity, I need to add another filtering level- adding a linkentity to current link entity, Is it possible? What is the way to write it?

This is my current code that works fine.

QueryExpression query = new QueryExpression("entity1");
query.Criteria.AddCondition(new ConditionExpression("statecode", ConditionOperator.Equal, 0));          
query.ColumnSet = new ColumnSet("entity1Id");
LinkEntity Link = new LinkEntity("entity1", "entity2", "entity2Id", "entity2Id", JoinOperator.LeftOuter);
Link.LinkCriteria.AddCondition(new ConditionExpression("statecode", ConditionOperator.Equal, 0));         
    Link.Columns = new ColumnSet("entity2Data");
     Link.EntityAlias = "entity2";
    query.LinkEntities.Add(Link);

for conclusion: I need to add filtering to entity2 according to entity3.

UPDATE:I added this code:

    QueryExpression query = new QueryExpression("entity1");
    query.Criteria.AddCondition(new ConditionExpression("statecode", ConditionOperator.Equal, 0));                            
    query.ColumnSet = new ColumnSet(true);
    LinkEntity portfolioLink = new LinkEntity("entity1", "entity2", "**entity2dI**", "entity2Id", JoinOperator.LeftOuter);
    portfolioLink.LinkCriteria.AddCondition(new ConditionExpression("statecode", ConditionOperator.Equal, 0));                   
query.LinkEntities.Add(portfolioLink);
LinkEntity portfolioLink2 = new LinkEntity("entity3", "entity2", "entity2Id", "entity2Id", JoinOperator.LeftOuter);
portfolioLink2.LinkCriteria.AddCondition(new ConditionExpression("statecode", ConditionOperator.Equal, 0));                   
query.LinkEntities.Add(portfolioLink2);
EntityCollection characterizationeedsCollection = Service.RetrieveMultiple(query);

I get an exception that entity1 don't recognize entity2Id field, **entity1 field's name is unfortunately a little different ** but it worked fine before adding the third entity

Upvotes: 1

Views: 1693

Answers (1)

It is same as 1st Link entity. In addition to existing code, add another Link entity like below:

        LinkEntity Link2 = new LinkEntity("entity2", "entity3", "entity3Id", "entity3Id", JoinOperator.LeftOuter);
        Link2.LinkCriteria.AddCondition(new ConditionExpression("statecode", ConditionOperator.Equal, 0));         
        Link2.Columns = new ColumnSet("entity3Data");
        Link2.EntityAlias = "entity3";
        query.LinkEntities.Add(Link2);

Upvotes: 1

Related Questions