Reputation: 649
I have a 1 to many foreign key related table and I am wondering how to create the following SQL Server query in a Entity Framework LINQ query to only find parent records that have child records?
SELECT p.ParentId ,p.ParentName
FROM Parent p JOIN Child c on p.ParentId = c.ParentId
GROUP BY p.ParentId, p.ParentName
HAVING COUNT(c.ParentId) > 0
Upvotes: 0
Views: 688
Reputation: 1494
Inner join already limits your resultset to rows where both parent and child are present, so something like below should work:
var parentsWithChildren = (
from parent in Parent
join child in Child on parent.ParentId equals child.ParentId
select new { parent.ParentId, parent.ParentName } ).Distinct();
Upvotes: 3