实体框架6 - 选择孩子平等的父母

时间:2014-05-09 18:10:53

标签: c# linq entity-framework-6

我只需要Parent对象。在SQL中,这很简单:

select distinct * from parent 
join child on child.ParentID = Parent.ID 
where child.playssoccer = true;

在实体框架6中,这似乎将原子分裂给我。

我需要新的p => parent.children.playssoccer = true的父母。

如何从类似的EF6 DBContext中获取足球父母?

3 个答案:

答案 0 :(得分:7)

from p in context.Parents
where p.Children.Any(c => c.PlaySoccer == true)
select p

假设您希望父母至少有一个孩子踢足球。

答案 1 :(得分:3)

如果您有导航属性,则可以执行类似

的操作
Parents.Where(p => p.child.playsoccer)

答案 2 :(得分:2)

Parents
.Where(p=> p.child.playsoccer)
.GroupBy(p=> p.Parent.ID)
相关问题