简体   繁体   中英

Entity Framework 6 - Select Parents Where Children Equal

I just need Parent objects. In SQL, this is simple:

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

In Entity Framework 6, this seems like splitting the atom to me.

I need new p => parent where parents.children.playssoccer = true .

How do I get soccer parents out of a similar EF6 DBContext?

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

This is assuming you want parents who have at least one child that plays soccer.

如果你有导航属性,你可以做类似的事情

Parents.Where(p => p.child.playsoccer)
Parents
.Where(p=> p.child.playsoccer)
.GroupBy(p=> p.Parent.ID)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM