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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:56:20+00:00 2026-05-17T02:56:20+00:00

Does NHibernate support inline views using criterias? Google doesn’t seem to return any relevant

  • 0

Does NHibernate support inline views using criterias? Google doesn’t seem to return any relevant results. Here is the query I need to convert preferably using criterias.

SELECT COUNT (incident_count) AS incident_count,
       SUM (total_customers) AS total_customers,
       MAX (longest_etr) AS longest_etr,
       COUNT (DISTINCT crew_count) AS crew_count
  FROM (SELECT   l.incident_id AS incident_count,
                 i.downstream_cust_qty_total AS total_customers,
                 TO_CHAR (MAX (l.etr_datetime),
                          'MM/DD/YYYY HH24:mi:ss'
                         ) AS longest_etr,
                 ca.crew_no AS crew_count
            FROM district d,
                 LOCATION l,
                 ZONE z,
                 incident_device ID,
                 incident i,
                 crew_action ca
           WHERE l.dist_no = d.dist_no
             AND d.zone_id NOT IN (1008, 1010)
             AND ID.location_id = l.location_id
             AND ID.incident_id = i.incident_id
             AND l.location_id = i.location_id
             AND ca.incident_id = i.incident_id
             AND ca.location_id = l.location_id
             AND ID.call_type_cd IN ('ELEC', 'PLAN')
             AND ID.clue_cd NOT IN (248, 258, 975)
             AND l.fac_job_status_cd IN ('A', 'D', 'F', 'G', 'P', 'U', 'W')
             AND z.zone_id = d.zone_id
             AND ca.crew_action_id = l.crew_action_id
             AND l.dist_no = 24
             AND l.primary_loc_flg = 'T'
        GROUP BY l.incident_id, i.downstream_cust_qty_total, ca.crew_no)

I already have everything converted in the where clause. That part was no problem. Which translates into something like.

GetSession().CreateCriteria(typeof (Incident), () => incidentAlias)
    // Projection
    .SetProjection(
        Projections.ProjectionList()
            .Add(LambdaProjection.Count<Incident>(i => incidentAlias.IncidentId).As(() => IncidentCount))
            .Add(LambdaProjection.Sum<Incident>(i => incidentAlias.DownstreamCustQtyTotal).As(() => TotalCustomers))
            .Add(LambdaProjection.Max<Location>(l => locationAlias.EtrDatetime).As(() => LongestEtr))
            .Add(LambdaProjection.CountDistinct<CrewAction>(ca => crewActionAlias.CrewNo).As(() => CrewCount))
            .Add(LambdaProjection.GroupProperty(() => incidentAlias.IncidentId))
            .Add(LambdaProjection.GroupProperty(() => incidentAlias.DownstreamCustQtyTotal))
            .Add(LambdaProjection.GroupProperty(() => crewActionAlias.CrewNo))
    )
    // Aliases
    .CreateAlias(() => incidentAlias.Locations, () => locationAlias)
    .CreateAlias(() => incidentAlias.IncidentDevices, () => incidentDeviceAlias)
    .CreateAlias(() => incidentAlias.District, () => districtAlias)
    .CreateAlias(() => districtAlias.Zone, () => zoneAlias)
    .CreateAlias(() => locationAlias.CrewAction, () => crewActionAlias)
    // Criterias
    .Add(() => locationAlias.PrimaryLocFlg == "T")
    .Add(() => locationAlias.DistNo == districtNumber)
    .Add(() => zoneAlias.ZoneId != 1008)
    .Add(() => zoneAlias.ZoneId != 1010)
    .Add(SqlExpression.In(() => locationAlias.FacJobStatusCd, new[] { "A", "D", "F", "G", "P", "U", "W" }))
    .Add(SqlExpression.In(() => incidentDeviceAlias.CallTypeCd, new [] { "ELEC", "PLAN" }))
    .Add(() => incidentDeviceAlias.ClueCd != "248")
    .Add(() => incidentDeviceAlias.ClueCd != "258")
    .Add(() => incidentDeviceAlias.ClueCd != "975")
    .SetResultTransformer(Transformers.AliasToBean<Dto>())
    .List<Dto>();

Note that I’m using the Lambda criteria extension. Alternatively, I suppose I could create an additional Dto to select all columns with no aggregate functions then use Linq to do the count/sum/max/count distinct.

  • 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-17T02:56:21+00:00Added an answer on May 17, 2026 at 2:56 am

    I just tried it with HQL and it does not work (would be the same with the Criteria API). What does work, however, is the following:

    select 
      (select count(*) from Table1 t1), 
      (select   sum(*) from Table2 t2) 
    from DummyTable dt
    where rownum <= 1
    

    DummyTable is not doing anything other than being there so NHibernate doesn’t cry about it, and rownum <= 1 is there to ensure NHibernate doesn’t try to return a list of objects. 😉

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

Sidebar

Related Questions

Does NHibernate support mapping of SQL VIEWS? Any examples would be helpful.
Does anyone have any good sources of information of using NHibernate with Sql Azure
Does Spring.NET have any support (integration) with NHibernate that would eneble per request ISession
Is there any architectural or philosophical reason why NHibernate does not have support for
I knew nHibernate does not support UNION. But there are some tricks, we can
Does the current version of NHibernate (v2.1.2) support access Oracle stored procedure output REFCURSOR
I'm interesting in following: does NHibernate support DetachedCriteria caching? I found a lot of
In Fluent NHibernate, References() returns an object which doesn't support the 'ReadOnly()' method. I'm
I know LINQ-to-NHibernate currently does not support sub-queries ( http://ayende.com/Blog/archive/2009/07/26/nhibernate-linq-1.0-released.aspx ). Is there any
I understand that nhibernate 3.1 does not support left join with the linq syntax

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.