I'm trying to apply the advice in this post: Tip 22 - How to make Include really Include
It suggests a workaround for ensure eager loading works in the Entity Framework (4.2). That workaround involves casting the IQueryable to an ObjectQuery.
However, when I attempt this, as shown in the post, the query returns a null.
My query is (ctx is a DbContext):
IEnumerable<Coupon> coupons =
from x in ctx.Coupons
where x.LanguageCode.Equals("EN", StringComparison.InvariantCultureIgnoreCase) && x.CategoryId == MainCategoryId && x.Date >= fromDate && x.Date <= toDate
select x;
That works as expected.
However, when I use,
IEnumerable<Coupon> coupons =
(from x in ctx.Coupons
where x.LanguageCode.Equals("EN", StringComparison.InvariantCultureIgnoreCase) && x.CategoryId == MainCategoryId && x.Date >= fromDate && x.Date <= toDate
select x) as ObjectQuery<Coupon>;
it assigns a null to "coupons".
Any idea what I'm doing wrong?
var
instead ofIEnumerable<Coupon>
?IQueryable<T>
andIEnumerable<T>
are not the same thing. – QuizIQueryable<T>
to anObjectQuery<T>
unless thatIQueryable<T>
really is anObjectQuery<T>
." I'm wondering why you really need that, though. In your query, you wouldn't need the workaround in the first place, but if you did, does(from x in ctx.Coupons ... select x).Include(...)
not do the trick, assuming you'reusing System.Data.Entity;
? – Jamie