仅返回子实体Linq实体框架

Eri*_* J. 3 linq-to-entities entity-framework ef-code-first

给出如下内容:

public class Parent
{
    public int Id { get; set; }
    public List<Child> Children { get; set; }
}

// There is no reference to the parent in the object model
public class Child
{
    public int Id { get; set; }
    public string MyProperty { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

是否有可能加载只有一个孩子符合特定条件,对于给定父ID,还没有加载父实体?

见过使用投影加载父项零个或多个匹配条件的子项的解决方案.

Joa*_*son 5

如果我理解正确的话,应该这样做; 它将您想要的条件放在父级和子级上,但只选择子级.

from parent in db.Parents
from child in parent.Children
where parent.Id = 4711 &&
      child.MyProperty == "olle"
select child;
Run Code Online (Sandbox Code Playgroud)