title: How do I apply the LINQ to SQL Distinct() operator to a List<T>?
body: <p>I have a serious(it's getting me crazy) problem with <strong>LINQ to SQL</strong>. I am developing an ASP.NET MVC3 application using c# and Razor in Visual Studio 2010.</p>
  
  <p>I have two database tables, <strong>Product</strong> and <strong>Categories</strong>:</p>
  
  <p><strong>Product(Prod_Id[primary key], other attributes)</strong></p>
  
  <p><strong>Categories((Dept_Id, Prod_Id) [primary keys], other attributes)</strong></p>
  
  <p>Obviously <strong>Prod_Id</strong> in Categories is a foreign key. Both classes are mapped using the Entity Framework (EF). I do not mention the context of the application for simplicity.</p>
  
  <p>In Categories there are multiple rows containing the <strong>Prod_Id</strong>. I want to make a projection of all <strong>Distinct  Prod_Id</strong> in Categories. I did it using plain (T)SQL in SQL Server MGMT Studio according to this (really simple) query:</p>
  
  <pre><code>SELECT DISTINCT Prod_Id
  FROM Categories
  </code></pre>
  
  <p>and the result is correct. Now I need to make this query in my application so I used:</p>
  
  <pre><code>var query = _StoreDB.Categories.Select(m =&gt; m.Prod_Id).Distinct();
  </code></pre>
  
  <p>I go to check the result of my query by using:</p>
  
  <pre><code>query.Select(m =&gt; m.Prod_Id);
  </code></pre>
  
  <p>or</p>
  
  <pre><code>foreach(var item in query)
  {
    item.Prod_Id;
    //other instructions
  }
  </code></pre>
  
  <p>and it does not work. First of all the Intellisense when I attempt to write <code>query.Select(m =&gt; m.</code> or <code>item.</code>shows just suggestions about methods (such as Equals, etc...) and not properties. I thought that maybe there was something wrong with Intellisense (I guess most of you many times hoped that Intellisense was wrong :-D) but when I launch the application I receive an error at runtime.</p>
  
  <p>Before giving your answer keep in mind that;</p>
  
  <ol>
  <li><p>I checked many forums, I tried the normal LINQ to SQL (without using lambdas) but it does not work. The fact that it works in (T)SQL means that there is something wrong with the LINQ to SQL instruction (other queries in my application work perfectly).</p></li>
  <li><p>For application related reasons, I used a <code>List&lt;T&gt;</code> variable instead of _StoreDB.Categories and I thought that was the problem. If you can offer me a solution without using a <code>List&lt;T&gt;</code> is appreciated as well.</p></li>
  </ol>