Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 621
John Dunagan Avatar asked Oct 17 '25 11:10

John Dunagan


3 Answers

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.

like image 69
Paul Abbott Avatar answered Oct 19 '25 01:10

Paul Abbott


If you have navigation properties you can do something like

Parents.Where(p => p.child.playsoccer)
like image 25
tympaniplayer Avatar answered Oct 19 '25 00:10

tympaniplayer


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.

like image 8
Paul Abbott Avatar answered Oct 19 '25 01:10

Paul Abbott