Entity Framework 6 - Select Parents Where Children Equal

c# entity-framework-6 linq

Question

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?

1
3
5/9/2014 7:20:48 PM

Accepted Answer

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.

7
5/9/2014 6:13:14 PM

Popular Answer

If you have navigation properties you can do something like

Parents.Where(p => p.child.playsoccer)


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow