I have a complex M:M:M query I can't figure out. My case: I need to retrieve all extra costs related to a trainee, from a particular training.
The relations are as follow:
- Trainee <-> Training M:M
- Trainee <-> Extra Costs M:M
- Training <-> Extra costs 1:M
My code currently looks like this but i don't even know if this is the way to go:
if (context.MessageName == "Update" && context.Stage == 20)
{
targetEntity = context.InputParameters["Target"] as Entity;
var courseID = targetEntity.Id;
QueryExpression Query = new QueryExpression { EntityName = "cref8_extrakost", ColumnSet = new ColumnSet(true) };
// LINK BETWEEN A TRAINING AND A CURSIST
LinkEntity TrainingCursist = new LinkEntity
{
LinkFromEntityName = "cref8_cursist",
LinkToEntityName = "cref8_opleiding_cref8_cursist",
LinkFromAttributeName = "cref8_cursistid",
LinkToAttributeName = "cref8_cursistid",
Columns = new ColumnSet(true),
JoinOperator = JoinOperator.Inner
};
TrainingCursist.LinkCriteria.AddCondition("cref8_opleidingid", ConditionOperator.Equal, courseID);
// LINK BETWEEN A CURSIST AND A EXTRA COST
LinkEntity ExtraCostCursist = new LinkEntity
{
LinkFromEntityName = "cref8_extrakost",
LinkToEntityName = "cref8_extrakost_cref8_cursist ",
LinkFromAttributeName = "cref8_extrakost",
LinkToAttributeName = "cref8_cursistid",
JoinOperator = JoinOperator.Inner,
Columns = new ColumnSet(true)
};
ExtraCostCursist.LinkCriteria.AddCondition("cref8_cursistid", ConditionOperator.Equal, "34b5de6b - 0758 - ec11 - 8f8f - 000d3aad43bd");
Query.LinkEntities.Add(TrainingCursist);
Query.LinkEntities.Add(ExtraCostCursist);
var collection = service.RetrieveMultiple(Query);
}
Any help would be appreciated
Best Regards,
Anthony