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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:54:43+00:00 2026-05-13T18:54:43+00:00

I am trying to efficiently work with parent & child (post & tags) entities.

  • 0

I am trying to efficiently work with parent & child (post & tags) entities. A sample of the code can be seen here: http://gist.github.com/297464

Using the following results in less than 10 distinct Post entities if any of Post contains more than 1 tag.

var posts = session
    .CreateCriteria<Post>()
    .SetMaxResults(10)
    .SetFetchMode("Tags", FetchMode.Eager)
    .List<Post>();

If I remove the .SetFetchMode line above, I get the 10 records I am looking for, but all of the Tag entities are queried and then filtered in memory.

It seems like I should be able to instruct NHiberate to either pass in a list of PostIds or do a join.

I am pretty new to NHiberate so it is also entirely possible I am going at this completely wrong.

Thanks,
Scott

  • 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-13T18:54:44+00:00Added an answer on May 13, 2026 at 6:54 pm

    The problem is that SetMaxResults is not applied to the number of root entities returned from the query, it is translated into a T-SQL TOP (in the case of SqlServer), which is applied to the result from a join query. Since there is one row in the result set for each child of a root entity the TOP will not have the desired effect.

    To achieve a limit on the number of root entities you could use a join query with a subquery that contains the SetMaxResults limit.

    // Select the ids of the first ten posts
    var subquery = DetachedCriteria.For<Post>()
        .SetMaxResults(10)
        .SetProjection(Projections.Property("Id"));
    
    // Do a join query with posts and tags, for the posts who's ids are
    // in the result of the subquery
    var posts = session.CreateCriteria<Post>()
        .SetFetchMode("Tags", FetchMode.Join)
        .Add(Subqueries.PropertyIn("Id", subquery))
        .SetResultTransformer(Transformers.DistinctRootEntity)
        .List<Post>();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to work out how to efficiently sort a huge dataset that won't
Trying to make a make generic select control that I can dynamically add elements
I am currently trying to find the parent of a parent of an element.
I have a parent Div : <div id=ctl00_MainContentAreaPlaceHolder_deliveryOpen class=delivery_open style=display: block;> //Child select Element
I'm trying to work out a sensible approach for designing a database where I
I'm new to BDD, and using SpecFlow I'm trying to work out an efficient,
I'm trying to work with Threadding and it seems to me like it's suspiciously
I was recently profiling an application trying to work out why certain operations were
I am trying to implement redraw regions with up to 3 regions but can't
I am trying to determine the best time efficient algorithm to accomplish the task

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.