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 :
Customer experience | Sales, Customer Insights,...
Suggested Answer

QueryExpression for N:N with Join

(0) ShareShare
ReportReport
Posted on by 40

Hi guys,

I have a case that I need to retrieve Extra Costs linked to a Trainee and from a specific Training. I know how to query for the Extra Costs of a Training and the Extra Costs of a Trainee. I then compare both lists and retrieve matching ones. That works but it is really a performance hit. How can i add a join to one of the query's to combine both? 

Here is the code i am running now:

//THIS SECTION RETURNS ME ALL EXTRA COSTS FROM A TRAINING
                    ConditionExpression ceOpleidingExtraKost = new ConditionExpression("cref8_extrakosttraining", ConditionOperator.Equal, targetEntity.Id);
                    QueryExpression qeOpleidingExtraKost = new QueryExpression("cref8_extrakost");
                    qeOpleidingExtraKost.ColumnSet = new ColumnSet(true);
                    qeOpleidingExtraKost.Criteria.AddCondition(ceOpleidingExtraKost);
                    EntityCollection opleidingExtraKostResult = service.RetrieveMultiple(qeOpleidingExtraKost);

                    //THIS SECTION RETURNS ME ALL EXTRA COSTS FROM A TRAINEE
                    var query = new QueryExpression()
                    {
                        EntityName = "cref8_extrakost",
                        ColumnSet = new ColumnSet(true),
                    };
                    var link = query.AddLink("cref8_extrakost_cref8_cursist", "cref8_extrakostid", "cref8_extrakostid");
                    link.LinkCriteria = new FilterExpression()
                    {
                        Conditions =
                                    {
                                        new ConditionExpression("cref8_cursistid", ConditionOperator.Equal, relatedEntity.Id)
                                    }
                    };
                    var cursistExtraKosts = service.RetrieveMultiple(query);

                    //COMPARE BOTH EXTRA COSTS COLLECTION TO RETRIEVE ONLY THE ONES FROM A TRAINEE AND A SPECIFIC TRAINING
                    double traineeExtraKostKost=0;
                    EntityCollection TraineeTrainingExtraKostsCollection = new EntityCollection();
                    foreach (var extrakost in opleidingExtraKostResult.Entities)
                    {
                        foreach (var traineeExtraKost in cursistExtraKosts.Entities)
                        {
                            if (traineeExtraKost.Id==extrakost.Id)
                            {
                                traineeExtraKostKost  = Convert.ToDouble(traineeExtraKost.Attributes.FirstOrDefault(q => q.Key == "cref8_prijs").Value);
                                TraineeTrainingExtraKostsCollection.Entities.Add(extrakost);
                            }
                        }
                        
                    }

I have the same question (0)
  • Suggested answer
    Bipin D365 Profile Picture
    28,985 Moderator on at

    Hi,

    One reason why it is taking more time for you is

    qeOpleidingExtraKost.ColumnSet = new ColumnSet(true);

    You are trying to retrieve all cloumns from that entity.

    This is not a best practice and it will have major performance hit on your instance.

    So I would recommend you change your code to fetch only required attribute -

    qeOpleidingExtraKost.ColumnSet = new ColumnSet("column1schemaname","column2schemaname");

    You would need to change your code @ line 4 & 12

    Please mark my answer verified if this is helpful!

    Regards,

    Bipin Kumar

    Follow my Blog: xrmdynamicscrm.wordpress.com/

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!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
ManoVerse Profile Picture

ManoVerse 180 Super User 2026 Season 1

#2
11manish Profile Picture

11manish 123

#3
CU11031447-0 Profile Picture

CU11031447-0 100

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans