需要帮助尝试在 EntityFramework 中 EagerLoad 一些数据

发布于 2024-09-19 07:14:46 字数 456 浏览 6 评论 0原文

我正在尝试对 EF 实体进行一些预先加载。

所以,如果实体名为 Orders .. 那么我想我会执行以下操作...

_someContext.Orders.Include("Whatever") ....

但是问题是,我有一个像下面这样的方法...

public IQueryable<Order> Find(Expression<Func<Order, bool>> predicate)
{
    return CurrentContext.Orders.Where(predicate);
}

效果很好..但是我可以利用 Expression 谓词将 Include("whatever") 包含在那里,而不必添加另一个方法参数?

I'm trying to do some eager loading on an EF Entity.

so, if the entity is called Orders .. then I guess i would do the following...

_someContext.Orders.Include("Whatever") ....

But the problem is, I have a method like the following ...

public IQueryable<Order> Find(Expression<Func<Order, bool>> predicate)
{
    return CurrentContext.Orders.Where(predicate);
}

which works great .. but can i leverage the Expression predicate to include the Include("whatever") in there, instead of having to add another method parameter?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小忆控 2024-09-26 07:14:47

我不这么认为。由于谓词和 ObjectQuery.Where 方法 一般来说,与 Include 的急切加载无关。虽然您可以创建一个扩展方法,但这并不能避免您使用另一个参数来指定包含:

public IQueryable<Order> Find(Expression<Func> predicate, string include) {
    return CurrentContext.Orders.Where(predicate, include);
}

public static ObjectQuery<T> Where<T>(this ObjectQuery<T> entity, Expression<Func<T, bool>> predicate, string include) {
    return (ObjectQuery<T>)entity.Include(include).Where<T>(predicate);
}

I don't think so. Since the predicate and ObjectQuery.Where Method in genral has nothing to do with eager loading by Include. You can create a extension method though, but that does not save you from having yet another parameter for specifying the include:

public IQueryable<Order> Find(Expression<Func> predicate, string include) {
    return CurrentContext.Orders.Where(predicate, include);
}

public static ObjectQuery<T> Where<T>(this ObjectQuery<T> entity, Expression<Func<T, bool>> predicate, string include) {
    return (ObjectQuery<T>)entity.Include(include).Where<T>(predicate);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文