Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy doesn't Distinct with Entity Framework do "DISTINCT" on SQL Server?
    primarykey
    data
    text
    <p>I just recently switched out our repository and context from Linq-to-SQL to Entity Framework 4.1 CodeFirst.</p> <p>All my unit tests are passing at the moment, except one! And that is the one that checks if I get a distinct result from a SQL-view.</p> <pre><code>var query = worker.ObjectRepository.GetFreeObjects(); query = query.Distinct(); return query; </code></pre> <p>When I inspect the query just before I call .Distinct(), the query is correct and it runs fine when I execute it manually on SQL Server.</p> <p>But after I call <code>.Distinct()</code>, the query just looks the exact same as before.</p> <p>If I manually add a DISTINCT to my SQL query, it works fine, but calling <code>.Distinct()</code> in code doesn't do this.</p> <p>Why is that? </p> <p><strong>Edit</strong></p> <pre><code>public IQueryable&lt;SearchResultValueObject&gt; GetFreeObjects() { return Get(Context.FreeObjects) } public IQueryable&lt;TEntity&gt; Get&lt;TEntity&gt;(IQueryable&lt;TEntity&gt; query) { return query; } </code></pre> <p>I simplyfied a bit becaus at this scenario that's all that is happening. Grabbing the data from the Context, and returning it.</p> <p>This is how my Context look</p> <pre><code>public class MyContext : DbContext, IMyContext { public virtual DbSet&lt;SearchResultValueObject&gt; SearchResult { get; set; } IQueryable&lt;SearchResultValueObject&gt; IMyContext.FreeObjects { get { return this.SearchResult; } } } </code></pre> <p>I also tried to remove everything except the objectNo from the SearchResultMap-file, I got the results from the Db, but <code>.Distinct()</code> didn't work.</p> <p><strong>Edit2</strong></p> <p>I made a simple example that has the same problem: <a href="http://pastebin.com/sYVfNb0Y" rel="nofollow">http://pastebin.com/sYVfNb0Y</a> Does this has something to do with the fact that I'm fetching from a view? Or am I missing something important here?</p> <p>Best regards Jesper</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload