Reputation: 177
I am working on MVC3 app, using Entity Framework 4.1. I did LINQ query, and returned it as IEnumerable<T>
. But, in controller, I have to use Include(other entity)
, like in ObjectSet
, so I can show in view other entity values (which is associated to the one I am doing query on). I tried explicit cast from IEnumerable<T>
to ObjectSet<T>
, but it throws exception. Is there any way I can get ObjectSet
from IEnumerable
, or to include entity in IEnumerable
?
Thanks in advance.
Upvotes: 1
Views: 772
Reputation: 18068
If your context is still alive (if you haven't left the using) and if you haven't done .ToList()
you should be able to cast to ObjectQuery<T>
.
A safer practice would be to use an extension method on IQueryable<T>
which checks if queryable is a ObjectQuery<T>
and if so casts to ObjectQuery<T>
and returns result .Include() otherwise returns the input query.
Upvotes: 2
Reputation: 7172
Once you have it cast to an IEnumerable, it becomes disconnected from the datasource.
You should include the related entities in the DAL while it is still an IQueryable or IObjectQuery and then return the complete result as an IEnumerable.
Otherwise, you will be doing an extra hit on the DB.
Si
Upvotes: 0