title: How to use LINQ Distinct() with multiple fields
body: <p>I have the following <strong>EF class</strong> derived from a database (simplified)</p>
  
  <pre><code>class Product
  { 
       public string ProductId;
       public string ProductName;
       public string CategoryId;
       public string CategoryName;
  }
  </code></pre>
  
  <p><code>ProductId</code> is the <strong>Primary Key</strong> of the table. </p>
  
  <p>For a bad design decision made by the DB designer (I cannot modify it), I have <code>CategoryId</code> and <code>CategoryName</code> in this table.</p>
  
  <p>I need a <strong>DropDownList</strong> with (distinct) <code>CategoryId</code> as <strong>Value</strong> and <code>CategoryName</code> as <strong>Text</strong>. Therefore I applied the following code:</p>
  
  <pre><code>product.Select(m =&gt; new {m.CategoryId, m.CategoryName}).Distinct();
  </code></pre>
  
  <p>which logically it should create an anonymous object with <code>CategoryId</code> and <code>CategoryName</code> as properties. The <code>Distinct()</code> guarantees that there are no duplicates pair (<code>CategoryId</code>, <code>CategoryName</code>).  </p>
  
  <p>But actually it does not work. As far as I understood the <code>Distinct()</code> works just when there is just one field in the collection otherwise it just ignores them...is it correct? Is there any workaround? Thanks!</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Sorry <code>product</code> is:</p>
  
  <pre><code>List&lt;Product&gt; product = new List&lt;Product&gt;();
  </code></pre>
  
  <p>I found an alternative way to get the same result as <code>Distinct()</code>:</p>
  
  <pre><code>product.GroupBy(d =&gt; new {d.CategoryId, d.CategoryName}) 
         .Select(m =&gt; new {m.Key.CategoryId, m.Key.CategoryName})
  </code></pre>