title: Query to return entities that match two link criterias with Left Outer Join
body: <p>I have a query that should return "A" entities while having links to entity "B" and "C" and these two links have "Left Outer Join" operator so that I can receive "A" entities either from one link or another. </p>
  
  <p>After execution it returns me only one record which is common for two of the link criterias. If I remove link to entity "B" then I get expected records that match the link criteria. Same happens when I remove link to entity "C" while the link to entity "B" is present.</p>
  
  <p>So, I assume that my query works only with one link, but it doesn't work as expected when there are two links and I don't understand why.</p>
  
  <p>Actually I was following this <a href="http://msdn.microsoft.com/en-us/library/dn531006.aspx" rel="nofollow">MSDN example</a>.</p>
  
  <p>Here's my data:</p>
  
  <pre><code>_____________
  |     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
  </code></pre>
  
  <p>My query expression:</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>The query expression gets transformed into following Fetch XML query:</p>
  
  <pre><code>&lt;fetch distinct="true" no-lock="false" mapping="logical"&gt;
      &lt;entity name="A"&gt;
          &lt;attribute name="a_id" /&gt;
          &lt;attribute name="a_name" /&gt;
          &lt;filter type="and"&gt;
              &lt;filter type="or"&gt;
                  &lt;!-- doesn't work as expected --&gt;
                  &lt;condition attribute="b_one_id" operator="not-null" entityname="connectionB" /&gt;
                  &lt;condition attribute="c_one_id" operator="not-null" entityname="connectionC" /&gt;
  
                  &lt;!-- doesn't work as expected either --&gt;
                  &lt;condition attribute="a_id" operator="not-null" entityname="A" /&gt;
              &lt;/filter&gt;
          &lt;/filter&gt;
          &lt;link-entity name="B" to="a_id" from="b_one_id" link-type="outer" alias="connectionB"&gt;
              &lt;filter type="and"&gt;
                  &lt;condition attribute="b_two_id" operator="eq" value="1" /&gt;
              &lt;/filter&gt;
          &lt;/link-entity&gt;
          &lt;link-entity name="C" to="a_id" from="c_one_id" link-type="outer" alias="connectionC"&gt;
              &lt;filter type="and"&gt;
                  &lt;condition attribute="c_two_id" operator="eq" value="2" /&gt;
              &lt;/filter&gt;
          &lt;/link-entity&gt;
      &lt;/entity&gt;
  &lt;/fetch&gt;
  </code></pre>