2014-11-14 102 views
1

我有一個查詢應該返回「A」實體,同時鏈接到實體「B」和「C」,這兩個鏈接有「左外連接」運算符這樣我就可以從一個鏈接或另一個鏈接接收「A」個實體。查詢返回匹配左外連接的兩個鏈接標準的實體

執行後,它只返回一條記錄,這條記錄對於兩條鏈接標準是通用的。如果我刪除實體「B」的鏈接,那麼我會得到符合鏈接條件的預期記錄。當我移除到實體「C」的鏈接而實體「B」的鏈接存在時,也會發生同樣的情況。

因此,我認爲我的查詢只適用於一個鏈接,但它不能按預期工作,當有兩個鏈接,我不明白爲什麼。

其實我是跟着這個MSDN example

這裏是我的數據:

_____________ 
|  A  | 
------------- 
| a_id | 
|  1  | - entity with this id is the only record in the results 
|  2  | - entity with this id should appear in the results, but it is not 
|  3  | - entity with this id should appear in the results, but it is not 

_________________________________ 
|    B    | 
--------------------------------- 
| b_one_id | b_two_id | 
|  1  |  1  | - matches link criteria with alias "connectionB" 
|  2  |  1  | - matches link criteria with alias "connectionB" 
|  1  |  444  | - doesn't match link criteria, should not appear 

_________________________________ 
|    C    | 
--------------------------------- 
| c_one_id | c_two_id | 
|  1  |  2  | - matches link criteria with alias "connectionC" 
|  3  |  2  | - matches link criteria with alias "connectionC" 
|  1  |  555  | - doesn't match link criteria, should not appear 

我的查詢表達式:

QueryExpression query = new QueryExpression 
{ 
    EntityName = A.EntityLogicalName, 
    ColumnSet = new ColumnSet 
    { 
     AllColumns = false, 
     Columns = 
     { 
      "a_id", 
      "a_name" 
     } 
    } 
}; 

query.Distinct = true; 

query.AddLink(B.EntityLogicalName, "a_id", "b_one_id", JoinOperator.LeftOuter); 
query.LinkEntities[0].EntityAlias = "connectionB"; 
query.LinkEntities[0].LinkCriteria.AddCondition("b_two_id", ConditionOperator.Equal, 1); 

query.AddLink(C.EntityLogicalName, "a_id", "c_one_id", JoinOperator.LeftOuter); 
query.LinkEntities[0].EntityAlias = "connectionC"; 
query.LinkEntities[0].LinkCriteria.AddCondition("c_two_id", ConditionOperator.Equal, 2); 

// doesn't work as expected 
query.Criteria.AddFilter(LogicalOperator.Or); 
query.Criteria.Filters[0].AddCondition("connectionB", "b_one_id", ConditionOperator.NotNull); 
query.Criteria.Filters[0].AddCondition("connectionC", "c_one_id", ConditionOperator.NotNull); 

// doesn't work as expected either 
query.Criteria.AddCondition("a_id", ConditionOperator.NotNull); 

查詢表達式被轉換成以下提取XML查詢:

<fetch distinct="true" no-lock="false" mapping="logical"> 
    <entity name="A"> 
     <attribute name="a_id" /> 
     <attribute name="a_name" /> 
     <filter type="and"> 
      <filter type="or"> 
       <!-- doesn't work as expected --> 
       <condition attribute="b_one_id" operator="not-null" entityname="connectionB" /> 
       <condition attribute="c_one_id" operator="not-null" entityname="connectionC" /> 

       <!-- doesn't work as expected either --> 
       <condition attribute="a_id" operator="not-null" entityname="A" /> 
      </filter> 
     </filter> 
     <link-entity name="B" to="a_id" from="b_one_id" link-type="outer" alias="connectionB"> 
      <filter type="and"> 
       <condition attribute="b_two_id" operator="eq" value="1" /> 
      </filter> 
     </link-entity> 
     <link-entity name="C" to="a_id" from="c_one_id" link-type="outer" alias="connectionC"> 
      <filter type="and"> 
       <condition attribute="c_two_id" operator="eq" value="2" /> 
      </filter> 
     </link-entity> 
    </entity> 
</fetch> 
+0

剛剛嘗試過,對我來說工作正常 - 有3排。你可以嘗試運行沒有任何過濾器,看看你運行的數據是否正確(我得到6行沒有過濾器:4行,其中a_id = 1,1其中a_id = 2,1其中a_id = 3)? – MarioZG

+0

你好,這是crm 2011還是crm 2013?什麼彙總? 如果這是crm 2011,那麼'outer join'就是以這種方式工作的: 如果您將relasionship與'outer'關聯起來,即使它們不符合您的條件,您也將檢索所有記錄。 – Sxntk

回答

0

我希望有一些編程代碼中的錯誤。 看看你的代碼部分

query.AddLink(B.EntityLogicalName, "a_id", "b_one_id", JoinOperator.LeftOuter); 
query.LinkEntities[0].EntityAlias = "connectionB"; 
query.LinkEntities[0].LinkCriteria.AddCondition("b_two_id", ConditionOperator.Equal, 1); 

query.AddLink(C.EntityLogicalName, "a_id", "c_one_id", JoinOperator.LeftOuter); 
query.LinkEntities[0].EntityAlias = "connectionC"; 
query.LinkEntities[0].LinkCriteria.AddCondition("c_two_id", ConditionOperator.Equal, 2); 

你加入query.AddLink(),然後再加入一些其他鏈接查詢 但使用相同的鏈接即query.LinkEntities [0] Alias和LinkCriteria。 可能是糾正這個錯誤可以解決你的問題。