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

The Archive Base Latest Questions

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

How can I map this SQL using NHibernate Criteria API? Sql: SELECT COUNT(*) FROM

  • 0

How can I map this SQL using NHibernate Criteria API?

Sql:

SELECT COUNT(*) FROM (
    SELECT FirstName, LastName FROM Employees GROUP BY FirstName, LastName
) AS Query

This is a very very simple query, my query has a SubSelect much more complex.

So, any idea?

  • 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-25T22:30:19+00:00Added an answer on May 25, 2026 at 10:30 pm

    I found a solution for the question, it’s a very big hack but it works as expected.

    I had to get the generated SQL and surround it with the SELECT COUNT(*) query. Here is the code to do that:

    public ISQLQuery BuildCountQuery(ICriteria criteria)
    {
        CriteriaImpl c = (CriteriaImpl)criteria;
        SessionImpl s = (SessionImpl)c.Session;
        string entityOrClassName = ExtractRealClassName(c);
    
        SessionFactoryImpl factory = (SessionFactoryImpl)s.SessionFactory;
        String[] implementors = factory.GetImplementors(entityOrClassName);
        string implementor = implementors.Length == 0 ? null : implementors[0];
        var persister = (IOuterJoinLoadable)factory.GetEntityPersister(implementor);
        CriteriaLoader loader = new CriteriaLoader(persister, factory, c, implementor, s.EnabledFilters);
        SqlString sql = loader.SqlString.Insert(0, "SELECT COUNT(*) FROM (");
        sql = sql.Append(") AS Query");
    
        var parameters = loader.Translator.CollectedParameters;
        var sqlQuery = this.session.CreateSQLQuery(sql.ToString());
        for (int i = 0; i < parameters.Count; i++)
            sqlQuery.SetParameter(i, parameters.ElementAt(i).Value, parameters.ElementAt(i).Type);
    
        return sqlQuery;
    }
    
    private string ExtractRealClassName(CriteriaImpl criteria)
    {
        Type rootEntityType = criteria.GetRootEntityTypeIfAvailable();
        if (rootEntityType.GetInterfaces().Contains(typeof(INHibernateProxy)))
            return criteria.GetRootEntityTypeIfAvailable().BaseType.FullName;
        else
            return criteria.EntityOrClassName;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Bash I can create a map (hashtable) with this common construction hput() {
We have several areas where code like this can be seen public Map extractData(ResultSet
can we ? i want to do something like this: google.maps.event.addListener(map, 'db_right_click', function(event) {
Can Automapper map values from a NameValueCollection onto an object, where target.Foo receives the
How can i map a date from a java object to a database with
Short version: How can I map two columns from table A and B if
I am using Nhibernate with Fluent, to persist a SQL Server 2008 express database
I am using java.sql.SQLData interface to map my java objects to Oracle database types.
Hello everybody With EF4, i can map a EDMX function (with update model from
Is this possible to map a SQL Native query (instead of a table) with

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.