I just recently switched out our repository and context from Linq-to-SQL to Entity Framework 4.1 CodeFirst.
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.
var query = worker.ObjectRepository.GetFreeObjects();
query = query.Distinct();
return query;
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.
But after I call .Distinct(), the query just looks the exact same as before.
If I manually add a DISTINCT to my SQL query, it works fine, but calling .Distinct() in code doesn’t do this.
Why is that?
Edit
public IQueryable<SearchResultValueObject> GetFreeObjects()
{
return Get(Context.FreeObjects)
}
public IQueryable<TEntity> Get<TEntity>(IQueryable<TEntity> query)
{
return query;
}
I simplyfied a bit becaus at this scenario that’s all that is happening. Grabbing the data from the Context, and returning it.
This is how my Context look
public class MyContext : DbContext, IMyContext
{
public virtual DbSet<SearchResultValueObject> SearchResult { get; set; }
IQueryable<SearchResultValueObject> IMyContext.FreeObjects { get { return this.SearchResult; } }
}
I also tried to remove everything except the objectNo from the SearchResultMap-file, I got the results from the Db, but .Distinct() didn’t work.
Edit2
I made a simple example that has the same problem: http://pastebin.com/sYVfNb0Y
Does this has something to do with the fact that I’m fetching from a view? Or am I missing something important here?
Best regards
Jesper
I would love it if someone could find a way around this. But after several hours of looking around it all came down to me making a new view based on the old view.
I don’t know if it’s the right way to do it, but it helped me with my problem.