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

  • Home
  • SEARCH
  • 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 5846117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:31:53+00:00 2026-05-22T12:31:53+00:00

I am trying to create a query using QueryOver , which will fetch a

  • 0

I am trying to create a query using QueryOver, which will fetch a collection using the Select or SubSelect mode. The entity in question is Track. I want to load a collection called TrackPrices, and I am doing this in the query:

q = q.Fetch(item => item.TrackPrices).Eager;

However, this creates a left join, which results in a problem for pagination. I would like it to perform a seperate select or subselect. Any idea if it can be done? As far as I know, using the criteria API one would do:

q.DetachedCriteria.SetFetchMode("TrackPrices", FetchMode.Select);

But I want to avoid magic strings in the code, thus I would prefer doing it using the QueryOver API.

  • 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-22T12:31:54+00:00Added an answer on May 22, 2026 at 12:31 pm

    From the time I posted this question, I’ve managed to find a workaround / solution which others might find useful as well.

    Basically, in such case you must first create another query which distinctly selects the primary keys of the main query, together with pagination. Since the DISTINCT(ID) will return only the results you want, you can use the pagination of SQL without any problems. Then, you re-run the main query, without pagination but using a condition where the ID is in one of the list returned. I created a general method which takes a criteria, looks up the IDs returned and adds them as a condition to the main criteria. Code below:

    public static void LimitCriteriaByPrimaryKeys(this NHibernate.ICriteria criteria, string primaryKeyName, int pageNum, int pageSize)
        {
            var session = NHManager.Instance.GetCurrentSessionFromContext();
            if (pageSize <= 0) pageSize = Int32.MaxValue - 1;
            var nhSession = NHManager.Instance.GetCurrentSessionFromContext();
            var pagingCriteria = (ICriteria)criteria.Clone();
            IList ids = null;
            var pKeyIDName = Projections.Property(primaryKeyName);  
            var pKeyProjection = Projections.Distinct(pKeyIDName); 
            {
                {
                    //paging
                    pagingCriteria.SetProjection(pKeyProjection); //sets the primary key distinct projection
                    if (pageSize > 0)
                    {
    
                        if (pageNum < 1)
                            pageNum = 1;
                        int skipAmt = (pageNum - 1) * pageSize;
                        pagingCriteria.SetFirstResult(skipAmt);
                        pagingCriteria.SetMaxResults(pageSize); 
    
                        ids = pagingCriteria.List(); //this returns the distinct list of IDs which should be returned for the given page & size
    
                    }
                }
            }
            {
                if (ids != null && ids.Count > 0)
                {
                    criteria.Add(Expression.In(pKeyIDName, ids));   //adds the primary key restriction
                    var crit = criteria;
                    crit.SetResultTransformer(new NHibernate.Transform.DistinctRootEntityResultTransformer());
                }
                else
                {
                    criteria.Add(Expression.Eq(pKeyIDName, 0)); //this is added specifically so that the main criteria returns NO results
                    criteria.Add(Expression.Eq(pKeyIDName, 1));
                }
            }
        }
    

    The methods NHManager.Instance.GetCurrentSessionFromContext(); can be replaced with your own method to retrieve the current session from the session factory.

    Hope it helps!

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

Sidebar

Related Questions

I am trying to create query which orders by child collection's property. It is
I am trying to create a report in SSRS2000 that will query an ORACLE
I am trying to create a not in query using hibernate Criteria. What I
I have 4 databases with similar schema's, and I'm trying to create a query
I'm trying to create a linq query based on some dynamic/optional arguments passed into
Trying to create several layers of folders at once C:\pie\applepie\recipies\ without using several different
I'm trying to write a query using these four simplified tables: Organization (pk) OrganizationID
I am trying to create a query where a user can enter: first name,
I'm trying to create a visualized chart using Jquery Highcharts plug-in. But I'm unable
I am trying to build a SQL query using ActiveRecord methods. The query has

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.