title: Retrieving Distinct Records using Query Expression in Microsoft CRM 2011
body: <p>I have a doubt in retrieving records in CRM 2011 using C# RetrieveMultiple method in a web service.</p>
  
  <p>I need to retrieve distinct records from an entity based on a attribute value(primary key).  I am able to achieve this by using the code below</p>
  
  <pre><code>QueryExpression query = new QueryExpression("entityname");
  query.ColumnSet.AddColumns("primarycolumn", "column1");
  query.Distinct = true;
  EntityCollection result1 = serviceProxy.RetrieveMultiple(query);
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>primarycolumn  column1
  xyz            1
  lmn            2 
  </code></pre>
  
  <p>This displays distinct records.  But if I add few more columns to the columnset, the results are not distinct.  This is shown in the code below</p>
  
  <pre><code>QueryExpression query = new QueryExpression("entityname");
  query.ColumnSet.AddColumns("primarycolumn", "column1", "column2");
  query.Distinct = true;
  EntityCollection result1 = serviceProxy.RetrieveMultiple(query);
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>primarycolumn  column1 column2 
  xyz            1        a        
  xyz            1        b 
  lmn            2        a
  </code></pre>
  
  <p>I need the result to be distinct only based on the primary column.</p>
  
  <p>Please help me in how to achieve this.</p>
  
  <hr>
  
  <ul>
  <li>More information on the above question.</li>
  </ul>
  
  <p>Actually, the code is in C#.   The table below shows the table which I am querying</p>
  
  <pre><code>**Primary
  column    Column1              Column2             Column3&lt;br/&gt;**
  
  
  
  Xyz            Value1              Value1               Value1 &lt;br/&gt;
  Xyz            Value2              Value2               Value2&lt;br/&gt;
  Lmn            Value1              Value1               Value1&lt;br/&gt;
  Lmn            Value2               Value2              Value2&lt;br/&gt;
  Xyz            Value1              Value1              Value1&lt;br/&gt;
  Lmn                      Value1             Value1               Value1&lt;br/&gt;
  </code></pre>
  
  <p>The query result should be like below.  Where only the primary columns distinct values must be considered and all other columns can be either distinct or not distinct.  Any one row of the distinct value of the primary column should only be displayed.</p>
  
  <pre><code>(This is the output I am trying to achieve)&lt;br/&gt;
  **Prmrycolumn   Column1 Column2 Column3&lt;br/&gt;**
  Xyz Value1  Value1  Value1&lt;br/&gt;
  Lmn Value1  Value1  Value1&lt;br/&gt;
  </code></pre>
  
  <p>The below code gives the following output</p>
  
  <pre><code>     QueryExpression query = new QueryExpression("entityname");
  query.ColumnSet.AddColumns("primarycolumn", "column1");
  
      query.Distinct = true;
      EntityCollection result1 = serviceProxy.RetrieveMultiple(query);
  
      **Primary column    Column1**&lt;br/&gt;
      Xyz Value1&lt;br/&gt;
      Lmn Value1&lt;br/&gt;
  </code></pre>
  
  <p>But as I add more columns to the columnset, the output is considering distinct values of other columns also</p>
  
  <pre><code>QueryExpression query = new QueryExpression("entityname");
  query.ColumnSet.AddColumns("primarycolumn", "column1", "column2");
  query.Distinct = true;
  EntityCollection result1 = serviceProxy.RetrieveMultiple(query);
  
  **Primary
  column  Column1 Column2 Column3&lt;br/&gt;**
  Xyz Value1  Value1  Value1&lt;br/&gt;
  Xyz Value2  Value2  Value2&lt;br/&gt;
  Lmn Value1  Value1  Value1&lt;br/&gt;
  Lmn Value2  Value2  Value2&lt;br/&gt;
  </code></pre>
  
  <p>ctually, the code is in C#. The table below shows the table which I am querying</p>
  
  <pre><code>**Primary
  column    Column1              Column2             Column3&lt;br/&gt;**
  
  
  
  Xyz            Value1              Value1               Value1 &lt;br/&gt;
  Xyz            Value2              Value2               Value2&lt;br/&gt;
  Lmn            Value1              Value1               Value1&lt;br/&gt;
  Lmn            Value2               Value2              Value2&lt;br/&gt;
  Xyz            Value1              Value1              Value1&lt;br/&gt;
  Lmn                      Value1             Value1               Value1&lt;br/&gt;
  </code></pre>
  
  <p>The query result should be like below. Where only the primary columns distinct values must be considered and all other columns can be either distinct or not distinct. Any one row of the distinct value of the primary column should only be displayed.</p>
  
  <pre><code>(This is the output I am trying to achieve)&lt;br/&gt;
  **Prmrycolumn   Column1 Column2 Column3&lt;br/&gt;**
  Xyz Value1  Value1  Value1&lt;br/&gt;
  Lmn Value1  Value1  Value1&lt;br/&gt;
  The below code gives the following output
  
  QueryExpression query = new QueryExpression("entityname");
  query.ColumnSet.AddColumns("primarycolumn", "column1");
  query.Distinct = true;
  EntityCollection result1 = serviceProxy.RetrieveMultiple(query);
  
  **Primary column    Column1**&lt;br/&gt;
  Xyz Value1&lt;br/&gt;
  Lmn Value1&lt;br/&gt;
  </code></pre>
  
  <p>But as I add more columns to the columnset, the output is considering distinct values of other columns also</p>
  
  <pre><code>QueryExpression query = new QueryExpression("entityname");
  query.ColumnSet.AddColumns("primarycolumn", "column1", "column2");
  query.Distinct = true;
  EntityCollection result1 = serviceProxy.RetrieveMultiple(query);
  
  **Primary
  column  Column1 Column2 Column3&lt;br/&gt;**
  Xyz Value1  Value1  Value1&lt;br/&gt;
  Xyz Value2  Value2  Value2&lt;br/&gt;
  Lmn Value1  Value1  Value1&lt;br/&gt;
  Lmn Value2  Value2  Value2&lt;br/&gt;
  </code></pre>