2017-07-21 28 views
0

我連接到動態CRM如下之後檢索結果:查詢查找財產屬性與「名」的商機實體 - Dynamics CRM Online中

CrmServiceClient crmConn = new CrmServiceClient(ConfigurationManager.ConnectionStrings["default"].ConnectionString); 
IOrganizationService crmService = crmConn.OrganizationServiceProxy; 

QueryExpression query = new QueryExpression("opportunity");  
query.Criteria = new FilterExpression(); 
query.Criteria.AddCondition("name", ConditionOperator.Like, "14%"); 

EntityCollection results = crmService.RetrieveMultiple(query); 

現在的名稱是字符串類型的,我能夠添加條件&執行操作&獲得結果。

我的要求是我要添加另一個過濾器,這是一個查找屬性「parentaccountid」。

我試圖添加如下條件,但它拋出拋出異常錯誤,因爲它只需要GUID。

query.Criteria.AddCondition("parentaccountid", ConditionOperator.Like, "%In%"); 

注:parentaccountid的類型是Microsoft.Xrm.Sdk.EntityReference當我從早期的結果

檢索的原因是,我們可以應用過濾器只能用於GUID parentaccountid。

有什麼辦法可以添加基於「名稱」而不是「ID」的條件?

+0

我嘗試了LinkEntities,但未能實現所需的功能。然而,@Dave Clark提供了我正在尋找的確切答案。謝謝戴夫。 – ashveli

回答

2

是,只需添加"name""parentaccountid"末:

query.Criteria.AddCondition("parentaccountidname", ConditionOperator.Like, "%In%"); 

你會發現,如果你創建一個高級查找這樣的:

enter image description here

然後下載FetchXML,CRM只需追加"name"到查找屬性名稱的末尾:

<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false"> 
    <entity name="opportunity"> 
    <attribute name="name" />  
    <order attribute="name" descending="false" /> 
    <filter type="and"> 
     <condition attribute="parentaccountidname" operator="like" value="%In%" /> 
    </filter> 
    </entity> 
</fetch> 

另一種解決方案是使用LinkEntity,並查詢鏈接帳戶的名稱。

+0

感謝戴夫這工作。但我嘗試使用LinkEntity進行查詢,但我無法找到此屬性的實體「parentaccountid」。所以,我無法加入opprtunity實體。基本上,parentaccountid是一個查找字段。如何在查找字段上使用LinkEntity?你可以用這個答案延長你的答案嗎? – ashveli

+0

嗨@ashveli,很高興我能幫上忙。我想回答你的問題,你需要提出一個新問題(按照SO的指導方針)。話雖如此,[LinkEntity](https://msdn.microsoft.com/en-us/library/microsoft.xrm.sdk.query.linkentity.aspx) –

+0

有很多博客帖子,甚至官方微軟帖子通過很多文章,但我無法找到如何查詢linkentity與查找屬性。我能找到的只是查詢機會和賬戶等實體。但是,機會和家長鼓勵我沒有運氣,直到你提供了一種方式。再次感謝。我認爲使用linkentity和lookup屬性來查詢是不可能的 – ashveli