2014-02-14 43 views
1

我在創建查詢時使用某些相關實體字段或其他相關實體字段時遇到了一些困難。我使用的是QueryExpression,因爲這是我最熟悉的,但如果有更好的方法來做到這一點,我完全贊成。嘗試使用OR查詢某些相關實體,並使用AND查詢其他相關實體

具體來說,我查詢的是BillTo.Name或Customer.Name等於傳入值的合同,並且相關聯的合同行上的一些自定義值等於傳入的值。這是我到目前爲止所嘗試的。問題在於它使用And運算符而不是Or處理BillToName和Customer名稱。

QueryExpression qe = new QueryExpression(Contract.EntityLogicalName); 

ColumnSet acctColumns = new ColumnSet("name"); 
LinkEntity acctlink = new LinkEntity("contract", "account", "customerid", "accountid", JoinOperator.Inner); 
acctlink.LinkCriteria.FilterOperator = LogicalOperator.Or; 
acctlink.LinkCriteria.AddCondition("name", ConditionOperator.Equal, CustName); 
acctlink.Columns = acctColumns; 
qe.LinkEntities.Add(acctlink); 

ColumnSet billToColumns = new ColumnSet("name"); 
LinkEntity billToLink = new LinkEntity("contract", "account", "billingcustomerid", "accountid", JoinOperator.Inner); 
billToLink.LinkCriteria.FilterOperator = LogicalOperator.Or; 
billToLink.LinkCriteria.AddCondition("name", ConditionOperator.Equal, CustName); 
billToLink.Columns = billToColumns; 
qe.LinkEntities.Add(billToLink); 

ColumnSet contractColumns = new ColumnSet("expireson"); 
LinkEntity contractLineLink = new LinkEntity("contract", "contractdetail", "contractid", "contractid", JoinOperator.Inner); 
contractLineLink.Columns = contractColumns; 

LinkEntity productLink = new LinkEntity("contractdetail", "product", "productid", "productid", JoinOperator.Inner); 
productLink.LinkCriteria.AddCondition("productnumber", ConditionOperator.Equal, ProductID); 
contractLineLink.LinkEntities.Add(productLink); 

qe.LinkEntities.Add(contractLineLink); 

FilterExpression fe = new FilterExpression(LogicalOperator.And); 
ConditionExpression ceVersion = new ConditionExpression("new_version", ConditionOperator.Equal, versionID); 
ConditionExpression ceCust = new ConditionExpression("new_cust", ConditionOperator.Equal, true); 
ConditionExpression ceComp = new ConditionExpression("new_comp", ConditionOperator.Like, formattedDBName); 

fe.AddCondition(ceVersion); 
fe.AddCondition(ceCust); 
fe.AddCondition(ceComp); 

qe.Criteria.AddFilter(fe); 
qe.ColumnSet = cs; 

EntityCollection contractDetails = crmService.RetrieveMultiple(qe); 

編輯 - 最終代碼 這裏是我最後通過達林提到的選項看完之後做的事情。

 Guid acctGuid = acctRef.Id; // Retrieve EntityReference in another method 
     ColumnSet cs = new ColumnSet(); 

     QueryExpression qe = new QueryExpression(Contract.EntityLogicalName); 

     ColumnSet contractColumns = new ColumnSet("expireson", "new_registrationkey"); 
     LinkEntity contractLineLink = new LinkEntity("contract", "contractdetail", "contractid", "contractid", JoinOperator.Inner); 
     contractLineLink.Columns = contractColumns; 

     LinkEntity productLink = new LinkEntity("contractdetail", "product", "productid", "productid", JoinOperator.Inner); 
     productLink.LinkCriteria.AddCondition("productnumber", ConditionOperator.Equal, ProductID); 
     contractLineLink.LinkEntities.Add(productLink); 

     qe.LinkEntities.Add(contractLineLink); 

     FilterExpression fe = new FilterExpression(LogicalOperator.And); 
     ConditionExpression ceVersion = new ConditionExpression("new_version", ConditionOperator.Equal, VersionID); 
     ConditionExpression ceCust = new ConditionExpression("new_cust", ConditionOperator.Equal, true); 
     ConditionExpression ceComp = new ConditionExpression("new_comp", ConditionOperator.Like, formattedName); 
     fe.AddCondition(ceVersion); 
     fe.AddCondition(ceCust); 
     fe.AddCondition(ceComp); 

     qe.Criteria.AddFilter(fe); 
     qe.ColumnSet = cs; 

     FilterExpression fe2 = new FilterExpression(LogicalOperator.Or); 
     ConditionExpression ceCustomerName = new ConditionExpression("customerid", ConditionOperator.Equal, acctGuid); 
     ConditionExpression ceBillToName = new ConditionExpression("billingcustomerid", ConditionOperator.Equal, acctGuid); 
     fe2.AddCondition(ceCustomerName); 
     fe2.AddCondition(ceBillToName); 
     qe.Criteria.AddFilter(fe2); 

     EntityCollection contractDetails = crmService.RetrieveMultiple(qe); 

回答

1

你有兩個選擇

  1. 更改InnerJoin年代到LeftOuter的,然後過濾在客戶端的結果。

  2. 執行兩個單獨的查詢,一個用於客戶ID的內部聯接,另一個用於帳單客戶ID的內部聯接,然後在客戶端組合它們。

FilterOperator = LogicalOperator.Or僅適用於ConditionExpression S上的過濾器內,因爲你只設置一個ConditionExpression它並沒有真正做任何事情。

+0

由於這是介意,我更新了一下我的查詢。我一直希望避免額外的旅行,但由於依賴它的選項之一,我決定去旅行,以獲得傳入名稱的指導,然後我可以使用它來簡化主要查詢。我會在主帖末尾發佈更新後的代碼,以防別人幫助其他人。 –

相關問題