Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7635769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:30:36+00:00 2026-05-31T07:30:36+00:00

I am trying to get a parent entity where all entities in a child

  • 0

I am trying to get a parent entity where all entities in a child collection are in another list.

For example:

public class Parent {

public virtual int Id {get;set;}
public virtual List<Child> Children {get;set;}

}

public class Child {

public virtual int Id {get;set;}
public virtual string Name {get;set;}

}

I’ve tried various combinations of Joins and Restrictions but can’t seem to hit the spot.

So please help with suggestions.

Current example below:

 public IList<Lead> GetAllAvailable(string[] names)
    { 
        var result =  Session.CreateCriteria<Parent>()
            .CreateCriteria("Children")
            .Add(Expression.In("Name", names)).List<Parent>();

        return result;
    }

Edit:

This is the sql equivilent:

select  *
from    dbo.Parent
        join ( select   p.id
               from     dbo.Parent p
                        join dbo.ParentToChildren on p.Id = dbo.ParentsToChildren.Parent_Id
                        join dbo.Child on dbo.ParentToChildren.Child_Id = dbo.Child.Id
               where    Name in ( 'foo', 'bar' )
               group by p.Id
               having   count(1) > 1
             ) as foo on dbo.Parent.Id = foo.Id
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T07:30:37+00:00Added an answer on May 31, 2026 at 7:30 am

    Here’s my suggestion:

    var parents = session.QueryOver<Child>()
      .WhereRestrictionOn(x => x.Name).IsIn(names)
      .Select(Projections.Group<Child>(x => x.Parent))
      .Where(Restrictions.Ge(Projections.Count<Child>(x => x.Parent), names.Length))
      .List<Parent>();
    

    The idea is as follows: find all children that have a Name like one of the names entries. Group those children by their Parent. Your Child will need a Parent property mapped to the respective parent for this, but that’s a good idea anyway. For all groups with a size equal to (or greater than, but that shouldn’t happen, so you could replace Ge with Eq) names.Length, return their parent; because if the size of the group is equal to names.Length, all names have been found assuming no two children of a parent have the same name.

    The generated query:

    SELECT
        this_.Parent as y0_
    FROM
        Child this_
    WHERE
        this_.Name in (
            /*  */
        )
    GROUP BY
        this_.Parent
    HAVING
        count(this_.Parent) >= /* names.Length */;
    

    I’ve created a test app that returned promising results.

    If you need to do more with the parents, like paging or fetching the children, you could split this problem into a sub query (note that the .Fetch(x=>x.Children).Eager line is not required, it’s just an example what you can further do with the query):

    var parentSubQuery =
      QueryOver.Of<Child>()
        .WhereRestrictionOn(x => x.Name).IsIn(names)
        .Select(Projections.Group<Child>(x => x.Parent))
        .Where(Restrictions.Ge(Projections.Count<Child>(x => x.Parent), names.Length));
    
    var parents = session.QueryOver<Parent>() 
      .Fetch(x=>x.Children).Eager // not necessary, just an example
      .WithSubquery.WhereProperty(x => x.Id).In(parentSubQuery )
      .List();
    

    SQL (without the Fetch):

    SELECT
        this_.Id as Id1_0_
    FROM
        Parent this_
    WHERE
        this_.Id in (
            SELECT
                this_0_.Parent as y0_
            FROM
                Child this_0_
            WHERE
                this_0_.Name in (
                    /* names */
                )
            GROUP BY
                this_0_.Parent
            HAVING
                count(this_0_.Parent) >= /* names.length */
        );
    

    Update:

    If Parent<->Child is many-to-many, things get a little bit trickier:

          Parent parent = null;
          var parentSubQuery = QueryOver.Of<Child>()
            .WhereRestrictionOn(x => x.Name).IsIn(names)
            .JoinQueryOver(x => x.Parents, () => parent)
            .Where(Restrictions.Ge(Projections.Count(() => parent.Id), names.Length))
            .Select(Projections.Group(() => parent.Id));
    
          var parents = session.QueryOver<Parent>()
            .WithSubquery.WhereProperty(x => x.Id).In(parentSubQuery)
            .List();
    

    The main difference is that instead of grouping by the direct Parent property of Child I first needed to join the parents collection. To reference each parent there I introduce an alias parent.

    The generated SQL is pretty close to the original approach:

    SELECT
        this_.Id as Id2_0_
    FROM
        Parent this_
    WHERE
        this_.Id in (
            SELECT
                parent1_.Id as y0_
            FROM
                Child this_0_
            inner join
                ChildToParent parents3_
                    on this_0_.Id=parents3_.ChildId
            inner join
                Parent parent1_
                    on parents3_.ParentId=parent1_.Id
            WHERE
                this_0_.Name in (
                    /* names */
                )
            GROUP BY
                parent1_.Id
            HAVING
                count(parent1_.Id) >= /* names.Length */
        );
    

    For my test scenario it works, so hopefully it will for you, too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get the index of child of its parent. For example:-
I have a parent entity (Treatment) with a collection of child entities (Segments). I
I'm trying out Entity Framework Code first CTP4. Suppose I have: public class Parent
I'm trying to create a parent entity (Policy) with two child-entity collections (ExpressionRules and
I have a object structure like this: public class Entity { IList<Relationship> Relationships{get;set;} }
I have a one-to-many mapping between a parent entity and child entities. Now I
Consider following class public class AccountGroup : Entity<int> { public AccountGroup() { Accounts =
Say I have the following POCO classes: public class Parent { public int ID
Basically I am trying to get the gallery of parent page, and I've been
I am trying get all html links within a string and replace them using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.