如何将 IQueryable转换为到 ObjectQuery?
我正在尝试应用这篇文章中的建议: 提示 22 - 如何使 Include 真正包含
它建议了一种解决方法,以确保急切加载在实体框架 (4.2) 中正常工作。该解决方法涉及将 IQueryable 转换为 ObjectQuery。
但是,当我尝试执行此操作时,如帖子中所示,查询返回 null。
我的查询是(ctx 是 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;
按预期工作。
但是,当我使用时,
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>;
它会为“优惠券”分配一个空值。
知道我做错了什么吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从评论到答案:
转换为。由于您使用的是
ObjectQuery
仅当查询确实是ObjectQuery
时才有效,它不适用于任何其他<代码>IQueryableDbContext
而不是ObjectContext
,因此查询属于不同类型。但是,您不需要转换为正确的类型,System.Data.Entity
命名空间中的DbExtensions.Include
扩展方法接受任何IQueryable类型。
类型,并调用适当的底层Include
方法。您可以使用它,避免强制转换,从而避免在代码中显式指定查询的类型。From the comments to an answer:
Casting to an
ObjectQuery<T>
only works when the query really is anObjectQuery<T>
, it won't work on any otherIQueryable<T>
. Since you're using aDbContext
instead of anObjectContext
, the queries are of a different type. However, you do not need to cast to the correct type, theDbExtensions.Include
extension methods in theSystem.Data.Entity
namespace accept anyIQueryable<T>
type, and call the appropriate underlyingInclude
method. You can use this, avoid the cast, and thereby avoid explicitly specifying the query's type in your code.您所做的与此等效
,其中
coupons
不为 null 并且converted
null。这意味着演员阵容失败。
这并不能解决您的问题,但至少可以确定它。
也许您正在使用不同版本的实体框架?
What you are doing is equivalent to this
, with
coupons
not null andconverted
null.Which means the cast is failing.
This does not solve your problem, but at least identifies it.
Perhaps you are using a different version of Entity Framework?
获取 SQL从实体框架查询 IQueryable 帮助我解决了这个问题,以满足稍微不同的要求,我需要访问 TraceString AND 底层参数,以便我可以注入一些
IQueryable<>
表达式转换为.SqlQuery<>()
这是 Steve Fenton 的代码,包含在扩展方法中:
现在您的代码可以如下所示:
Getting the SQL Query From an Entity Framework IQueryable helped me solve this issue to satisfy a slightly different requirement, I need to access the TraceString AND the underlying parameters so I could inject a few
IQueryable<>
expressions into a.SqlQuery<>()
Here's Steve Fenton's code wrapped into an Extension Method:
Now your code can look like this: