title: Dynamics CRM QueryExpression
body: <p>I have this query that populates a joborder with the notes (2 different types) that comes from the account on the job order. The problem that I'm having is that it works on most of the job orders but there are some that I'm noticing that the notes are not populating to. It seems to me that the account entity and internal notes are a one to many relationship, however I must be going about this all wrong. I also have below the FetchXML that I modeled my query from.</p>
  
  <pre class="lang-csh prettyprint-override"><code>  private  void PopulateInternalNotes()
    {
        EntityReference accountId = _Entity.GetAttributeValue&lt;EntityReference&gt;("hc_account");
        EntityReference buisnessId = _Entity.GetAttributeValue&lt;EntityReference&gt;("hc_businessunit");
  
        if(accountId == null || buisnessId ==null)
        {
            return;
        }
        _tracer.Trace("buisness unit: " + buisnessId);
       //to get internal description
        QueryExpression exp = new QueryExpression("hc_internalnote");
  
        exp.Criteria.AddCondition("hc_internalnotetype", ConditionOperator.Equal, 948050000); // internal description option
        exp.Criteria.AddCondition("hc_account", ConditionOperator.Equal, accountId.Id);// matches account on joborder
        exp.Criteria.AddCondition("hc_businessunit", ConditionOperator.Equal, buisnessId.Id);//matches buisnessunit on joborder
  
        exp.ColumnSet = new ColumnSet("hc_note");
        EntityCollection results = _service.RetrieveMultiple(exp);
        if (results.Entities.Count == 1)
        {
            foreach (Entity r in results.Entities)
            {
                _tracer.Trace("one  internal desc found");
                _Entity["hc_internaldescription"] = r.GetAttributeValue&lt;string&gt;("hc_note");
  
            }
        }
        else
        {
            _tracer.Trace("no internal desc found");
        }
  
        //to get submission process note
        QueryExpression sub = new QueryExpression("hc_internalnote");
  
  
        sub.Criteria.AddCondition("hc_internalnotetype", ConditionOperator.Equal, 948050002); // submission process  option
        sub.Criteria.AddCondition("hc_account", ConditionOperator.Equal, accountId.Id);// matches account on joborder
        sub.Criteria.AddCondition("hc_businessunit", ConditionOperator.Equal, buisnessId.Id);//matches buisnessunit on joborder
  
        sub.ColumnSet = new ColumnSet("hc_note");
        EntityCollection Subresults = _service.RetrieveMultiple(sub);
        if (Subresults.Entities.Count == 1)
        {
            foreach (Entity s in Subresults.Entities)
            {
                _tracer.Trace("one submission desc found");
                _Entity["hc_submissionprocessnotes"] = s.GetAttributeValue&lt;string&gt;("hc_note");
  
            }
        }
        else
        {
            _tracer.Trace("more than one submission note found");
        }
  
    }
  </code></pre>
  
  <p>FetchXML below</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;fetch distinct="false" mapping="logical" output-format="xml-platform" version="1.0"&gt;
    &lt;entity name="hc_internalnote"&gt;
      &lt;attribute name="createdon"/&gt;
      &lt;attribute name="statecode"/&gt;
      &lt;attribute name="ownerid"/&gt;
      &lt;attribute name="hc_note"/&gt;
      &lt;attribute name="modifiedon"/&gt;
      &lt;attribute name="modifiedby"/&gt;
      &lt;attribute name="hc_internalnotetype"/&gt;
      &lt;attribute name="createdby"/&gt;
      &lt;attribute name="hc_contract"/&gt;
      &lt;attribute name="hc_businessunit"/&gt;
      &lt;attribute name="hc_account"/&gt;
      &lt;attribute name="hc_internalnoteid"/&gt;
      &lt;order descending="false" attribute="createdon"/&gt;
      &lt;filter type="and"&gt;
        &lt;condition attribute="hc_account" value="{B1F13E37-2D34-E411-9518-005056010301}" uitype="account" uiname="Community Memorial Hospital (050394)" operator="eq"/&gt;
      &lt;/filter&gt;
    &lt;/entity&gt;
  &lt;/fetch&gt;
  </code></pre>