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 8500903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:04:06+00:00 2026-06-11T01:04:06+00:00

I have 3 classes with one to many relationships A has many B has

  • 0

I have 3 classes with one to many relationships

A has many B has many C

I want to load all of my data where either of two conditions are true:

B.someField is in a list of strings

C.someOtherField is in that same list of strings

Sometimes B will be the match, sometimes C will be the match. I need to load all matches, and load them for the full levels.

In other words, if B.someField matches, I want to load that B, and its parent A, and all children C.

Likewise, if C.someOtherField matches, I want to load its parent B and B’s parent A.

Is there an efficient way make this query? How would you do it in nHibernate?

  • 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-06-11T01:04:07+00:00Added an answer on June 11, 2026 at 1:04 am

    When quering over multiple child collections using NHibernate, I tend to prefer to use the LINQ query syntax over lambdas because not only is it much easier to write but the code looks cleaner.

    Take this example which should point you in the right direction:

    var listOfString = new List<string>() { "String1", "String2" };
    
    var customers =
    (
        from a in session.Query<A>()
        from b in a.B
        from c in b.C
        where a.Status == "Active"
                && listOfStrings.Contains( b.SomeField )
                && listOfStrings.Contains( c.someOtherField )
        select a )
        .ToList();
    

    By using the LINQ query syntax, we can easily add multiple FROMs to gain access to the child collections. Then it’s just a matter of apply your WHERE conditions. For your particular case, you would simply use LINQ’s Contains() method which equates to SQL’s IN operator.

    It also sounds like you are wanting to load all the child collections into memory as well so be sure to use NHibernate’s .Fetch() method to eagerly load that data like so:

    var customers =
    (
        from a in session.Query<A>()
        from b in a.B
        from c in b.C
        where a.Status == "Active"
                && listOfStrings.Contains( b.SomeField )
                && listOfStrings.Contains( c.someOtherField )
        select a )
        .Fetch( x => x.B )
        .ThenFetchMany( x => x.First().C )
        .ToList();
    

    FYI, using the .First() inside the .ThenFetchMany() method above is a trick I learned from the NHibernate’s Jira issue tracker website: https://nhibernate.jira.com/browse/NH-2972?focusedCommentId=22150&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-22150

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

Sidebar

Related Questions

I have a one-to-many relationship between two classes for this situation. I have a
I have two domain classes: Contract and Orgainisation . A contract has one contractor
I have two classes university and department, suppose there is one to many relationship
I have two classes with one-to-many relationship. For example: class User has_many :numbers ...
I have two classes, the Group class has a many to many relationship with
I have a few model classes with basic one-to-many relationships. For example, a book
I have two classes in an owned one-to-many relationship. The parent is Map, and
there are two dataobject classes A and B having one-to-many relationship. I want to
I have one abstract class and many child classes. In child classes are from
I have two classes one of which inherits from the other. Im trying to

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.