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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:30:25+00:00 2026-05-20T15:30:25+00:00

I have a model called ‘BusinessPage’ which can be associated with 1 or more

  • 0

I have a model called ‘BusinessPage’ which can be associated with 1 or more ‘BusinessPageCategories’. So I’ve got a 3rd table called ‘BusinessPagesInCategories’ that links these two.

I am trying to get a paged recordset of BusinessPages, along with the total number of records (I may have 100 rows in total but only return 20 at a time, but I pass back an int containing that total number in addition to the 20 rows).

The problem I have is that, since I implemented the Many-To-Many relationship, it was bringing back the same BusinessPage row x times (where x is the number of categories that BusinessPage is associated with). I only want a single instance of each business page to be returned. So I added the following to my NHibernate Criteria to give Distinct BusinessPage results.

.SetResultTransformer(CriteriaSpecification.DistinctRootEntity);

Whilst this works for me, in that I’m now correctly getting a single row per BusinessPage (and my Categories property within each BusinessPage contains the correct x categories that it’s assigned to), my row count is not reflecting that ‘distinct’ count – instead, if I have just 1 BusinessPage record which is associated with 3 categories, the row count is 3!?!

BusinessPage (Model)

public virtual int BusinessPageId { get; private set; }
public virtual IList<BusinessPageCategory> Categories { get; set; }
public virtual string BusinessName { get; set; }

BusinessPageCategory (Model)

public virtual int CategoryId { get; private set; }
public virtual string CategoryName { get; set; }

BusinessPagesInCategories (SQL Table used to relate the two models)

BusinessPageId
CategoryId

Here is the code I use to get my ‘paged business pages’:

public virtual IList<BusinessPage> GetPagedBusinessPages(int pageNumber, int pageSize, out int totalRecordCount)
{
    ICriteria c = CreateCriteria_BusinessPage()

        // we only want distinct business pages returned
        .SetResultTransformer(CriteriaSpecification.DistinctRootEntity);

    return c.PagedResults<BusinessPage>(pageNumber, pageSize, out totalRecordCount);
}

protected virtual ICriteria CreateCriteria_BusinessPage()
{
    return Session
        .CreateCriteria<BusinessPage>()
        .AddOrder(Order.Asc("BusinessName"));
}

And here are my extension methods for getting both the paged results AND the all important row count.

public static IList<T> PagedResults<T>(this ICriteria criteria, int pageNumber, int pageSize, out int totalRecordCount)
{
    var results = criteria
        .CloneNewCriteria()
        .SetPaging(pageNumber, pageSize)
        .Future<T>();

        totalRecordCount = criteria.GetTotalCount().Value;

        return results.ToList<T>();
}

public static ICriteria CloneNewCriteria(this ICriteria criteria)
{
    return CriteriaTransformer.Clone(criteria);
}

public static ICriteria SetPaging(this ICriteria criteria, int pageNumber, int pageSize)
{
    return criteria
        .SetMaxResults(pageSize)
        .SetFirstResult((pageNumber - 1) * pageSize);
}

public static IFutureValue<int> GetTotalCount(this ICriteria criteria)
{
    criteria.ClearOrders();

    return criteria
        .SetProjection(Projections.RowCount())
        .FutureValue<int>();
}

I’m still very new to NHibernate so there may be a simple solution to this?

  • 1 1 Answer
  • 1 View
  • 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-20T15:30:25+00:00Added an answer on May 20, 2026 at 3:30 pm

    Problem solved. It turns out that I was setting up an Alias to the Categories regardless of whether I need to filter by category or not, therefore, the join was established in a scenario I didn’t need it to be, and as a result was bringing back the duplicate rows (one for each join onto the categories)!

    In the scenario whereby I do need to pass in a categoryid and filter my results on that category, there are no duplicate rows via the joins as each businesspage can be assigned any 1 category just the once.

    I thought it was strange that it was making this join to categories even though I wasn’t wanting to reference the categories object until after I had the initial records returned, but it looks like creating that alias outside of the scope required, forced the join and the loading of those categories unnecessarily.

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

Sidebar

Related Questions

I have a model called Notifications, which basically acts as a join table between
I have a model called NoteCategory, which is functioning as a join table between
I have a model called meetings which will be used to store details of
I have a model called Contact which has_one guest like so. class Contact <
I have a model called Answer which has a ForeignKey relationship to another model
I have a model called Category which looks like this: class Category < ActiveRecord::Base
I have a model called shipments. I added some columns to the shipments table
I have a model called a voip_phone that has a an attribute schedule which
I have model called test, and test can have many tests, and should be
I have a Model called Section which has many articles ( Article ). These

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.