希望這將幫助:
private static bool RelationshipExists(IOrganizationService service, string relationshipname, Guid entity1Id, string entity1Name, Guid entity2Id, string entity2Name)
{
string relationship1EtityName = string.Format("{0}id", entity1Name);
string relationship2EntityName = string.Format("{0}id", entity2Name);
//This check is added for self-referenced relationships
if (entity1Name.Equals(entity2Name, StringComparison.InvariantCultureIgnoreCase))
{
relationship1EtityName = string.Format("{0}idone", entity1Name);
relationship1EtityName = string.Format("{0}idtwo", entity1Name);
}
QueryExpression query = new QueryExpression(entity1Name) { ColumnSet = new ColumnSet(false) };
LinkEntity link = query.AddLink(relationshipname,
string.Format("{0}id", entity1Name), relationship1EtityName);
link.LinkCriteria.AddCondition(relationship1EtityName,
ConditionOperator.Equal, new object[] { entity1Id });
link.LinkCriteria.AddCondition(relationship2EntityName,
ConditionOperator.Equal, new object[] { entity2Id });
return service.RetrieveMultiple(query).Entities.Count != 0;
}
對不起,我不太明白這一點。我假設xyz將持有相關顧問的集合。我相信這不會是一個準確的檢查,如果現有的xyz包含顧問A,B和C,而我的新相關顧問是D,E和F,該怎麼辦?我們不是在這裏只檢查這個聯繫人是否與顧問有任何關係,但我們實際上並沒有檢查現有的關係? – user3340627
嗨,只是編輯了代碼。不過,您需要進行更改以使其正常工作,因爲我目前沒有開發者設置。我也喜歡@Andrii Butenko提供的解決方案。我相信該解決方案將對您有所幫助,因爲這是編譯代碼 – Scorpion
謝謝您的回答。我無法使用「linqContext」這是一個內置對象嗎?對不起,我是一個CRM初學者 – user3340627