我有两个与N:N关系的自定义实体。 1)会员资格,2)优惠
我在会员和提议表格上使用了子网格来显示相关记录。
我正在编写一个插件函数,其中我有一个成员资格实体记录ID作为输入,我想获得Offer实体的所有相关记录作为输出。 我尝试过以下代码,但无法获得相关的优惠实体记录。
entity1 =“new_membership”,entity2 =“new_offer”,relationshipEntityName =“new_new_membership_new_offer”
public List<new_offer> getAllOffersFromMembership(string entity1,string entity2, string relationshipEntityName, string Id)
{
QueryExpression query = new QueryExpression(entity1);
query.ColumnSet = new ColumnSet(true);
LinkEntity linkEntity1 = new LinkEntity(entity1, relationshipEntityName, "new_membershipid","new_membershipid", JoinOperator.Inner);
LinkEntity linkEntity2 = new LinkEntity(relationshipEntityName, entity2, "new_offerid","new_offerid", JoinOperator.Inner);
linkEntity1.LinkEntities.Add(linkEntity2);
query.LinkEntities.Add(linkEntity1);
linkEntity2.LinkCriteria = new FilterExpression();
linkEntity2.LinkCriteria.AddCondition(new ConditionExpression( “new_membershipid”, ConditionOperator.Equal, Id));
EntityCollection collRecords = service.RetrieveMultiple(query);
//To Do : Get offer entity records
}
由于我是MS CRM的新手,可能是我在做一些愚蠢的错误。
提前致谢。
答案 0 :(得分:2)
反转您的查询以获取相关记录。
var query = new QueryExpression("new_offer"){ColumnSet = new ColumnSet(true)};
var offerLinkEntity = new LinkEntity("new_offer", "new_new_membership_new_offer","new_offerid", "new_offerid", JoinOperator.Inner);
var memebershipLinkEntity = new LinkEntity("new_new_membership_new_offer", "new_membership","new_membershipid","new_membershipid", JoinOperator.Inner) {LinkCriteria = new FilterExpression()};
memebershipLinkEntity.LinkCriteria.AddCondition(new ConditionExpression("new_membershipid",ConditionOperator.Equal, mem.Id));
offerLinkEntity.LinkEntities.Add(memebershipLinkEntity);
query.LinkEntities.Add(offerLinkEntity);
var response = service.RetrieveMultiple(query);
var offers = response.Entities ?? Enumerable.Empty<new_offer>();