检索 N:N 关系动态 CRM/Dataverse C# 插件

Retrieve N:N relationship Dynamics CRM/Dataverse C# Plugin

我有一个似乎无法自己解决的问题。我有多种情况需要检索 N:N 仅包含关系一侧 ID 的数据。

示例:我需要找到 N:N 培训的所有学员。我有培训编号,如何查询?

我尝试了以下方法: 在这个例子中:
cursist = Trainee
opleiding = 训练

if (context.MessageName == "Update" && context.Stage == 20)
    {
        targetEntity = context.InputParameters["Target"] as Entity;

        List<Guid> cursistID = new List<Guid>();
        QueryExpression query = new QueryExpression("cref8_cursist");
        query.ColumnSet = new ColumnSet(new string[] { "cref8_cursistid" });

        LinkEntity linkEntity1 = new LinkEntity("cref8_cursist", "cref8_Opleiding_cref8_Cursist_cref8_Cursi", "cref8_cursistid", "cref8_cursistid", JoinOperator.Inner);
        LinkEntity linkEntity2 = new LinkEntity("cref8_Opleiding_cref8_Cursist_cref8_Cursi", "cref8_opleiding", "cref8_opleidingid", "cref8_opleidingid", JoinOperator.Inner);

        linkEntity1.LinkEntities.Add(linkEntity2);
        query.LinkEntities.Add(linkEntity1);

        linkEntity2.LinkCriteria = new FilterExpression();
        linkEntity2.LinkCriteria.AddCondition(new ConditionExpression("cref8_opleidingid", ConditionOperator.Equal, targetEntity.Id));

        EntityCollection cursistCollection = service.RetrieveMultiple(query);
        if (cursistCollection.Entities.Count > 0)
        {
            foreach (var cursist in cursistCollection.Entities)
            {
                //DO SOME LOGIC
            }
        }

    }

最终出现以下错误: '在 MetadataCache 中找不到名称 = 'cref8_Opleiding_cref8_Cursist_cref8_Cursi' 且名称映射 = 'Logical' 的实体。

最好的问候,安东尼

这里只需要一个LinkEntity。

var query = new QueryExpression("cref8_cursist");
query.ColumnSet.AddColumn("cref8_cursistid");
var intersection = new LinkEntity
{
    LinkFromEntityName = "cref8_cursist",
    LinkToEntityName = "cref8_Opleiding_cref8_Cursist_cref8_Cursi",
    LinkFromAttributeName = "cref8_cursistid",
    LinkToAttributeName = "cref8_cursistid",
    JoinOperator = JoinOperator.Inner
};
intersection.LinkCriteria.AddCondition("cref8_opleidingid", ConditionOperator.Equal, targetEntity.Id);
query.LinkEntities.Add(intersection);
var cursistCollection = service.RetrieveMultiple(query);

还要确保您与这个名字 cref8_Opleiding_cref8_Cursist_cref8_Cursi

有 N:N 关系