简体   繁体   English

检索 N:N 关系 Dynamics CRM

[英]Retrieve N:N relationship Dynamics CRM

I have a relationship between Opportunities and my custom Contract entity in Dynamics 2016 on premise.我在内部部署的 Dynamics 2016 中的机会和我的自定义合同实体之间存在关系。 I am trying to retrieve all of the related contracts from a particular opportunity in a C# plugin.我正在尝试从 C# 插件中的特定机会中检索所有相关合同。 When I try to retrieve the relationships, I receive the error:当我尝试检索关系时,收到错误消息:

No system many-to-many relationship exists between opportunity and ccseq_contract.机会和 ccseq_contract 之间不存在系统多对多关系。 If attempting to link through a custom many-to-many relationship ensure that you provide the from and to attributes.如果尝试通过自定义多对多关系进行链接,请确保提供 from 和 to 属性。

It appears that the relationship does exist based on this screenshot:根据此屏幕截图,这种关系似乎确实存在:

N:N 关系定义

Here's my Query Expression:这是我的查询表达式:

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}
                    }
                }
            }
        }
    }
});

Why am I receiving this error and how can I resolve the error?为什么我会收到此错误以及如何解决此错误?

The LinkedEntity in a query expression is exactly like a SQL inner or outer join (you specify the join type).查询表达式中的LinkedEntity与 SQL 内连接或外连接(您指定连接类型)完全相同。 it's great for fetching a N:1 relationship, it doesn't really work for a N:N.它非常适合获取 N:1 关系,但它不适用于 N:N。

For the N:N, you need to go via the 'relationship entity'.对于 N:N,您需要通过“关系实体”。

If you want all contracts linked to an opportunity, you must retrieve all contacts that has a row linking them to that opportunity, in the 'relationship entity' table, 'ccseq_opportunity_ccseq_contract' (I'm using string constants below, because I don't quite know how you're building your entity classes).如果您希望将所有合同链接到一个机会,您必须在“关系实体”表“ccseq_opportunity_ccseq_contract”(我在下面使用字符串常量,因为我没有非常了解您如何构建实体类)。

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)                    
                }
            }
        }
    }       
};

As an aside, when you're not using the 'in' query operator, I would really prefer using LINQ queries instead of query expressions, if you have generated strongly typed entity classes.顺便说一句,当您不使用“in”查询运算符时,如果您生成了强类型实体类,我真的更喜欢使用 LINQ 查询而不是查询表达式。 The LINQ query would look like 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();
}

Here is where I ended up.这是我结束的地方。 This was based partially on gnud's answer.这部分基于 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);

Another answer that can make the logic of the query more understandable visually.另一个可以使查询逻辑在视觉上更易于理解的答案。 Think like in sql, first switch to intermediate table, then switch to other table像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)
                                }
                            }
                        }
                    }
                }
            }
        };

Please try to retrieve list of contracts using the below XML query.请尝试使用以下 XML 查询检索合同列表。 The query is done on the N:N relationship.查询是在 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>

Hope it helps.希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM