Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 检索N:N关系动态CRM_C#_Dynamics Crm_Microsoft Dynamics - Fatal编程技术网

C# 检索N:N关系动态CRM

C# 检索N:N关系动态CRM,c#,dynamics-crm,microsoft-dynamics,C#,Dynamics Crm,Microsoft Dynamics,我在2016年Dynamics on premise中的Opportunities和我的定制合同实体之间存在关系。我试图从C插件中的特定opportunity中检索所有相关合同。当我尝试检索关系时,收到错误: opportunity和ccseq_合同之间不存在系统多对多关系。如果试图通过自定义多对多关系进行链接,请确保提供from和to属性 根据此屏幕截图,该关系似乎确实存在: 下面是我的查询表达式: EntityCollection contracts = service.RetrieveMu

我在2016年Dynamics on premise中的Opportunities和我的定制合同实体之间存在关系。我试图从C插件中的特定opportunity中检索所有相关合同。当我尝试检索关系时,收到错误:

opportunity和ccseq_合同之间不存在系统多对多关系。如果试图通过自定义多对多关系进行链接,请确保提供from和to属性

根据此屏幕截图,该关系似乎确实存在:

下面是我的查询表达式:

EntityCollection contracts = service.RetrieveMultiple(new QueryExpression()
{
    EntityName = Opportunity.LogicalName,
    ColumnSet = new ColumnSet(new String[]
    {
        Opportunity.Properties.OpportunityId
    }),
    LinkEntities =
    {
        new LinkEntity
        {
            LinkFromEntityName = Opportunity.LogicalName,
            LinkToEntityName = Contract.LogicalName,
            LinkCriteria = new FilterExpression
            {
                FilterOperator = LogicalOperator.And,
                Conditions =
                {
                    new ConditionExpression
                    {
                        AttributeName = Opportunity.Properties.OpportunityId,
                        Operator = ConditionOperator.Equal,
                        Values = {wonOpportunity.Id}
                    }
                }
            }
        }
    }
});
为什么会收到此错误以及如何解决此错误?

查询表达式中的LinkedEntity与指定联接类型的SQL内部联接或外部联接完全相同。 这对于获取N:1关系很好,但对于N:N关系来说,它并不真正有效

对于N:N,您需要通过“关系实体”

如果希望所有合同都链接到opportunity, 您必须在“关系实体”表“ccseq_opportunity_ccseq_contract”中检索所有有一行链接到该opportunity的联系人。我在下面使用字符串常量,因为我不太清楚您是如何构建实体类的

var q = new QueryExpression("ccseq_contract") {
    ColumnSet = new ColumnSet(true), //or specify what fields you want from ccseq_contract
    LinkEntities =  {
        new LinkEntity() {              
            LinkFromEntityName = "ccseq_contract",
            LinkToEntityName = "ccseq_opportunity_ccseq_contract",
            ColumnSet = new ColumnSet(false), //don't fetch any fields from the link table
            LinkCriteria = new FilterExpression() {
                FilterOperator = LogicalOperator.And,
                Conditions = {
                    new ConditionExpression("opportunityid", ConditionOperator.Equal, wonOpportunity.Id)                    
                }
            }
        }
    }       
};
另一方面,当您不使用“in”查询操作符时,如果您已经生成了强类型实体类,我更喜欢使用LINQ查询而不是查询表达式。 LINQ查询看起来像

using(var ctx = new OrganizationServiceContext(service)) {
    var contracts = (
        from c in ctx.CreateQuery<ccseq_contract>()
        join lnk in ctx.CreateQuery<ccseq_opportunity_ccseq_contract>() on c.ccseq_contractId equals link.ccseq_contractId
        where lnk.opportunityid = wonOpportunity.Id
        select c
        // Or, to fetch only some fields, do 
        // select new { c.ccseq_contractId, c.ccseq_name }
        ).ToList();
}

请尝试使用下面的XML查询检索合同列表。查询是在N:N关系上完成的

<fetch  mapping='logical'>
    <entity name='ccseq_opportunity_ccseq_contract'>
        <attribute name='opportunityid'/>
        <attribute name='ccseq_contractid'/>
        <link-entity name='opportunity' to='opportunityid' from='opportunityid' alias='opportunity'>
            <attribute name='opportunityid'/>
            <filter type='and'>
                <condition attribute='opportunityid' operator='eq' value=$'{wonOpportunity.Id}'/>
            </filter>
        </link-entity>
    </entity>
</fetch>

希望能有帮助。

这就是我的结局。这部分是基于gnud的回答

QueryExpression query = new QueryExpression("ccseq_opportunity_ccseq_contract");
query.ColumnSet.AddColumns(Contract.Properties.ContractId, Opportunity.Properties.OpportunityId);
query.Criteria = new FilterExpression();
query.Criteria.AddCondition(Opportunity.Properties.OpportunityId, ConditionOperator.Equal, wonOpportunity.Id);

EntityCollection contracts = service.RetrieveMultiple(query);

另一个答案可以使查询的逻辑更加直观易懂。就像在sql中一样,首先切换到中间表,然后切换到其他表

        QueryExpression query = new QueryExpression("ccseq_contract")
        {
            ColumnSet = new ColumnSet(true),
            LinkEntities =
            {
                new LinkEntity
                {
                    LinkFromEntityName = "ccseq_contract",
                    LinkToEntityName = "ccseq_opportunity_ccseq_contract",
                    LinkFromAttributeName = "ccseq_contractId",
                    LinkToAttributeName = "ccseq_contractId",
                    LinkEntities =
                    {
                        new LinkEntity
                        {
                            LinkFromEntityName = "ccseq_opportunity_ccseq_contract",
                            LinkToEntityName = "opportunity",
                            LinkFromAttributeName = "opportunityid",
                            LinkToAttributeName = "opportunityid",
                            LinkCriteria = new FilterExpression
                            {
                                Conditions =
                                {
                                    new ConditionExpression("opportunityid", ConditionOperator.Equal, wonOpportunity.Id)
                                }
                            }
                        }
                    }
                }
            }
        };

如果您只需要合同ID,这很好。如果每个协定都需要更多的内容,那么将协定表连接到链接表要比执行另一个查询来加载协定属性要高效得多。重要说明:intersect实体返回的Id是Guid类型,而不是EntityReference。。。有点奇怪。