检索动力学Crm中的N:N关系

时间:2014-06-16 09:58:16

标签: dynamics-crm-2011 dynamics-crm-4 dynamics-crm-2013

货币实体关联N:N与Lead的关系。现在我试图通过lead.i获取问题交易货币确实包含带有leadid.could的属性引用plz ...

if (context.InputParameters.Contains("Target") && (context.InputParameters["Target"]) is Entity)
{
    Entity en = (Entity)context.InputParameters["Target"];
    QueryExpression _Query = new QueryExpression
    {
        EntityName = "transactioncurrency",
        ColumnSet = new ColumnSet(true),
        LinkEntities = 
        { 
            new LinkEntity 
            { 
                Columns=new ColumnSet(true),
                LinkFromEntityName="lead",
                LinkFromAttributeName="leadid",
                LinkToEntityName="new_lead_transactioncurrency",
                LinkToAttributeName="transactioncurrencyid",
                LinkCriteria=new FilterExpression  
                {
                    FilterOperator=LogicalOperator.And,
                    Conditions=
                    { 
                        new ConditionExpression  
                        {
                            AttributeName="transactioncurrencyid",
                            Operator=ConditionOperator.Equal,
                            Values={ en.Id }
                        }
                    }
                }
            }
        }
    };

    EntityCollection entitycollect = service.RetrieveMultiple(_Query);
    int entitycount = entitycollect.Entities.Count;
}

1 个答案:

答案 0 :(得分:3)

我认为您需要确定正确的LinkFromAttributeNameLinkToAttributeName

QueryExpression query = new QueryExpression(entity1);

query.ColumnSet = new ColumnSet(true);

LinkEntity linkEntity1 = new LinkEntity("lead", "new_lead_transactioncurrency", "leadid", "leadid", JoinOperator.Inner);

LinkEntity linkEntity2 = new LinkEntity("new_lead_transactioncurrency", "transactioncurrency", "transactioncurrencyid", "transactioncurrencyid", JoinOperator.Inner);

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

// Add condition to match the Bike name with “Honda”
linkEntity2.LinkCriteria = new FilterExpression();

linkEntity2.LinkCriteria.AddCondition(new ConditionExpression("raj_bikename", ConditionOperator.Equal, "Honda"));

EntityCollection collRecords = service.RetrieveMultiple(query);

供参考,请查看以下链接:

Retrieve associated records (N:N related)

Sample: Convert queries between Fetch and QueryExpression