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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:42:28+00:00 2026-05-22T00:42:28+00:00

Using NHibernate, I need to be able to configure my application to sort a

  • 0

Using NHibernate, I need to be able to configure my application to sort a specific collection of entities exactly as needed.

The configurable sort:

  • can involve multiple properties
  • can specify the sorted properties in any order
  • can specify asc/desc on the sorted properties
  • can sort by custom properties (i.e. there is no corresponding SQL/C# property – it is calculated)

This functionality is inherited from an existing app where parts of the SQL are specified by an administrator and the SQL statement is built/executed dynamically.

Every time I try thinking through a solution I start getting in muddy waters with all kinds of alarms going off in my head regarding maintainability, performance, scalability, security, etc..

For example, I figure the admin can specify a comma delimited string like so:

“Date asc, FirstName asc, LastName desc”

I can split the string and go through a loop matching the property/sort pairings in a case statement and calling .AddOrder(Order.Asc(“FirstName”)) as necessary. But then, how do I handle custom properties? I could allow the user to specify SQL for calculating custom properties and then allow the user to sort on those like they would FirstName, but I’m seemingly back at dirty/kludge again.

Is there a clean/appropriate way to handle this requirement?

  • 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-22T00:42:29+00:00Added an answer on May 22, 2026 at 12:42 am

    After much thought and a stroke of luck, I may have a solution.

    public class CustomOrder : Order
    {
      private string customOrderSql;
    
      public CustomOrder(string customOrderSql) : base("", true)
      {
        this.customOrderSql = customOrderSql;
      }
    
    public override NHibernate.SqlCommand.SqlString ToSqlString(
        ICriteria criteria, ICriteriaQuery criteriaQuery) 
      { 
        return new NHibernate.SqlCommand.SqlString(this.customOrderSql); 
      }
    
    }
    

    I can pass a custom sort string to my repository where I add my CustomOrder as follows:

    .AddOrder(new CustomOrder(customSort))
    

    I still can’t sort by custom properties but maybe I can get away with applying case statements in the order by clause. I’m still open for better suggestions if they exist.

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

Sidebar

Related Questions

We need to make our enterprise ASP.NET/NHibernate browser-based application able to function when connected
I'm using NHibernate on a project and I need to do data auditing. I
I am using NHibernate as my ORM solution and have a need for a
I need to do a union of two tables using NHibernate and HQL. I
Using NHibernate ICriteria and adding .AddOrder ... I want to sort by a property
I'm currently working on an ASP.NET MVC project using NHibernate and I need to
I have this WPF application using NHibernate and lazy data loading. I also use
Using NHibernate I need to insert an entity into a database that has a
Background: I am using Nhibernate in an ASP.NET MVC application with an open-session-in-view pattern
Using NHibernate from C# and only HQL (not SQL) in a way that is

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.