EF构建了EntityCollection,但我(我想)我想要IQueryable

Lar*_*nal 9 entity-framework iqueryable

我有一个A具有简单导航属性的实体B.对于任何给定的实例A,我们期望几个相关的千个实例B.

没有我称之为的情况:

foreach(var x in A.B) { ... }
Run Code Online (Sandbox Code Playgroud)

相反,我对进行聚合操作感兴趣

var statY = A.B.Where(o => o.Property == "Y");
var statZ = A.B.Where(o => o.CreateDate > DateTime.Now.AddDays(-1));
Run Code Online (Sandbox Code Playgroud)

据我所知,EF实例化了数千个对B的引用,并在内存中执行这些操作.这是因为导航属性使用EntityCollection.相反,我希望它尽可能在SQL级别执行这些查询.

我目前的预感是导航属性可能不是正确的方法.我不喜欢EF,所以我对其他方法持开放态度.但是如果可能的话,我很想知道在EF下正确的方法.

(我正在使用EF4.)

Lar*_*nal 12

CreateSourceQuery似乎可以解决这个问题.

所以我的例子现在是:

var statY = A.B.CreateSourceQuery().Where(o => o.Property == "Y");
var statZ = A.B.CreateSourceQuery().Where(o => o.CreateDate > DateTime.Now.AddDays(-1));
Run Code Online (Sandbox Code Playgroud)