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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:11:08+00:00 2026-05-27T15:11:08+00:00

I have such problem which seems to be common, but in fact none of

  • 0

I have such problem which seems to be common, but in fact none of solutions from SO and google work for me.

Lets consider such situation (very similiar to mine):

    public class User
    {
     public int Id;
     public string Name;
     //30 other fields;
     public IList<Role> Roles;
    }

    public class Role
    {
     public int RoleNumber;
     public int UserId;
    }

1 User can have 0-many roles.

I want to display on gridview list of users with its fields and additional “Roles” column which will display all his RoleNumbers (using repeater). To obtain that, I need to get distincted list of Users with its Roles.

Is it possible to create such query with QueryOver ? All the solutions from internet say that I should create subquery which will Ids and then second query which will return Users which Id is in subquery. It works and returns me distincted list, but without Roles, so NH generates additional select for each user to get that missing information.

My Query:

Role roles = null;
User users = null;
IQueryOver<User,User> userQuery = ... 
IQueryOver<User,User> distQuery = ...
distQuery.JoinAlias(f=> f.Roles, () => Roles, JoinType.LeftOuterJoin)
         .Select(Projections.Distinct(Projections.Id()));

userQuery.WithSubquery.WhereProperty(g => g.Id).In<User>((QueryOver<User>)    distQuery) //and other joins, wheres etc.

returnes distincted list without Roles

If I add 2nd Join to Roles it will return list with Roles and duplicates

If I add 2nd Join to Roles and use

TransformUsing(new DistinctRootEntityResultTransformer())

It will return distincted list with Users and it roles but paging won’t work and RowCount() will return incorrect number.

Any ideas what can I do ?

  • 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-27T15:11:09+00:00Added an answer on May 27, 2026 at 3:11 pm

    Basically what you are asking for is not currently supported with NHibernate. You cannot eager load Users and Roles and keep pagination in check.

    I would first get a paged list of users (e.g. 25) then lazy load the roles as and when you need them.

    Session.QueryOver<User>().Skip(x).Take(25);
    

    and change your mapping file to:-

    <bag class="Role" Name="Roles" batch-size='25' ...>
    ...
    </bag>
    

    This would send one query to retrieve a list of paged Users then issue a second query to get all roles for the users. This is by far the most performant way.

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

Sidebar

Related Questions

I have a problem such that i have zip files uploaded from forms and
So I've got what seems to be a common problem, which is that the
I've got a problem which seems simple at first, but really isn't. I'm storing
I have some problem whith such mysql_query INSERT INTO table VALUES ('', CURDATE()-1) why
I have such a basic problem in Delphi,I can't solve it. My Code: Note:DataR
I have some complicated, problem to be solved. Now I need to write such
I'm elaborating over a problem which seems hard to me and I'm not expecting
From what I've seen in the past, StackOverflow seems to like programming challenges, such
This isn't a show-stopping programming problem as such, but perhaps more of a design
have such zend query: $select = $this->_table ->select() ->where('title LIKE ?', '%'.$searchWord.'%') ->where('description LIKE

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.