如何懒惰地查询集合导航属性 [英] How to lazily query a collection navigation property

查看:64
本文介绍了如何懒惰地查询集合导航属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我必须遵循POCO时

When I have to following POCO

class Poco {
  public int Id {get; set;}
  public virtual ICollection<Child> Children {get; set;}
}

和孩子

class Child {
  public int MyProperty {get; set;}
}

例如,如果他们满足某些谓词,我想得到一些孩子

and I want to get some children if they satisfy some predicate, for example

Poco mypoco = getMyPoco();
IEnumerable<Child> someChildren = mypoco.Children
                                  .Where( child => child.MyProperty > 30);

我注意到这首先从数据库中获取所有子项,然后对返回的列表进行过滤.如何确保条件在数据库而不是在应用程序中运行?

I'm noticing that this fetches all children from the database first, and then filters on the returned list. How can I ensure that the condition is run on the database rather than in my application?

推荐答案

您可以使用Load方法使用显式加载,但是需要暂时禁用惰性加载.

You can use explicit load using Load method, but the lazy loading needs to be disabled for temporary.

var context = ...; // DbContext
context.Configuration.LazyLoadingEnabled = false;

context.Entry(mypoco)
   .Collection(poco => poco.Children)
   .Query() 
   .Where(child => child.MyProperty > 30)
   .Load();

过滤器将在数据库中完成并加载到Children.

The filter will be done in database and loaded into Children.

这篇关于如何懒惰地查询集合导航属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆