Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Performance 如何延迟查询集合导航属性_Performance_Entity Framework_Linq To Entities - Fatal编程技术网

Performance 如何延迟查询集合导航属性

Performance 如何延迟查询集合导航属性,performance,entity-framework,linq-to-entities,Performance,Entity Framework,Linq To Entities,当我不得不跟随POCO的时候 class Poco { public int Id {get; set;} public virtual ICollection<Child> Children {get; set;} } 我想得到一些子类,如果它们满足某个谓词,例如 Poco mypoco = getMyPoco(); IEnumerable<Child> someChildren = mypoco.Children

当我不得不跟随POCO的时候

class Poco {
  public int Id {get; set;}
  public virtual ICollection<Child> Children {get; set;}
}
我想得到一些子类,如果它们满足某个谓词,例如

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

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

您可以使用
load
方法使用显式加载,但需要为临时设置禁用延迟加载

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

context.Entry(mypoco)
   .Collection(poco => poco.Children)
   .Query() 
   .Where(child => child.MyProperty > 30)
   .Load();
该筛选将在数据库中完成,并加载到
子项中

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

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