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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:12:25+00:00 2026-05-21T17:12:25+00:00

The data structure is as follows: A house has many rooms. Each room has

  • 0

The data structure is as follows:
A house has many rooms. Each room has many persons.

What I want to do is to get all persons for a house. In plain SQL I would write the following:

SELECT * FROM Person WHERE Room_id
IN
(SELECT Id FROM Room WHERE House_id = 1)

How can I write that in Fluent NHibernate’ish code?

For this example, we can assume that the entities and mappings look like this:

House entity

public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual IEnumerable<Room> Rooms { get; set; }

House mapping

Id(x => x.Id);
Map(x => x.Name);
HasMany(x => x.Rooms);

Room entity

public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual House House { get; set; }
public virtual IEnumerable<Person> Persons { get; set; }

Room mapping

Id(x => x.Id);
Map(x => x.Name);
References(x => x.House);
HasMany(x => x.Persons);

Person entity

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

Person mapping

Id(x => x.Id);
Map(x => x.Name);
References(x => x.Room);
  • 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-21T17:12:26+00:00Added an answer on May 21, 2026 at 5:12 pm

    To get SQL query close to yours you can use these criterias:

    var subCriteria = DetachedCriteria.For<Room>(); // subquery
    subCriteria.Add(Expression.Eq("House", house)); // where clause in subquery
    subCriteria.SetProjection(Projections.Id()); // DetachedCriteria needs to have a projection, id of Room is projected here
    
    var criteria = session.CreateCriteria<Person>();
    criteria.Add(Subqueries.PropertyIn("Room", subCriteria)); // in operator to search in detached criteria
    var result = criteria.List<Person>();
    

    This produces something like this:

    SELECT this_.Id as Id4_0_, this_.Name as Name4_0_, this_.RoomId as RoomId4_0_
    FROM [Person] this_
    WHERE this_.RoomId in (SELECT this_0_.Id as y0_ FROM [Room] this_0_ WHERE this_0_.HouseId = @p0)',N'@p0 int',@p0=1
    

    I tested it in FNH1.2 and NH3.1 but it should work well in NH2.1 as well.

    EDIT:
    UpTheCreek is right. Linq is more clear than Criteria API. For example:

    var query = session.Query<Person>().Where(x => x.Room.House == house);
    var linqResult = query.ToList<Person>();
    

    which produces different SQL query but result set is the same:

    select person0_.Id as Id4_, person0_.Name as Name4_, person0_.Room_id as Room3_4_
    from [Person] person0_, [Room] room1_
    where person0_.Room_id=room1_.Id and room1_.House_id=2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a python data-structure as follows: A = [{'abc': 'kjkjl'},{'abc': 'hjhjh'},{'abc': '78787'}] How
Yet another data structure doubt. I will get straight to it. This is what
Let's say that I want a class that represents a data structure in memory.
Let's say I have a binary tree data structure defined as follows type 'a
I have a data structure as follows: I have a list of objects with
I need a dictionary-like data structure that stores information as follows: key [value 1]
I built a stack data structure, which has a peek method. The compiler is
I'm looking for a data structure that has the following properties. Stores a list
I want to design a data structure with the following properties: Accessible as a
I'm trying to get a Method at runtime and then use its data structure

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.