0

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 
2
  • Please add a sample input and output data. Commented Oct 28, 2014 at 8:34
  • Can you share your EF data-model? Commented Oct 28, 2014 at 8:52

1 Answer 1

3

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();

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that's cleaner than I thought it would be.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.