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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:20:40+00:00 2026-05-31T18:20:40+00:00

Im trying to replicate the following sql in Nhibernate ICriteria SELECT DISTINCT AP.ID FROM

  • 0

Im trying to replicate the following sql in Nhibernate ICriteria

    SELECT DISTINCT AP.ID
FROM ActivityParts AP
    INNER JOIN ActivityBookings AB ON AB.ActivityPartID = AP.ID
    INNER JOIN OrderPartBookings OPB ON OPB.ActivityBookingID = AB.ID
    INNER JOIN OrderParts OP ON OP.ID = OPB.OrderPartID
    INNER JOIN Orders O ON O.ID = OP.OrderID
    LEFT JOIN Invoices I ON I.ID = (SELECT TOP 1 ID FROM Invoices WHERE OrderReferenceID = OP.ID AND Cancelled = 0 ORDER BY Created DESC)
WHERE
    O.OrderStatus != 'Cancelled'
    AND OP.Payed = 0
    AND (I.ID IS NOT NULL AND DATEADD(Day,1, I.DueDate) < @date)

So far i have managed to get the following (Note: I havent profiled it so im not quite sure how similar the sql becomes through the criteria):

var invoiceSubCrit = DetachedCriteria.For<Invoice>()
            .Add(Restrictions.Eq("Cancelled", false))
            .Add(Restrictions.EqProperty("OrderReference", "OP.ID"))
            .AddOrder(NhOrder.Desc("Created"))
            .SetProjection(Projections.Property("DueDate"))
            .SetMaxResults(1);

        var crit = Session.CreateCriteria<OrderPart>("OP")
            .CreateCriteria("OP.Bookings", "AB", JoinType.InnerJoin)
            .CreateCriteria("OP.Order", "O", JoinType.InnerJoin)
            .CreateCriteria("AB.ActivityPart", "AP", JoinType.InnerJoin)
            .Add(NhExpression.Eq("AP.ID", ActivityID))
            .Add(NhExpression.Eq("OP.Payed", false))
            .Add(NhExpression.Not(NhExpression.Eq("O.OrderStatus", OrderStatus.Cancelled)))
            .Add(NhExpression.Not(NhExpression.Eq("AB.Status", QueueStatus.Reserve)))
            .Add(Subqueries.Lt(DateTime.Now.Date.AddDays(1), invoiceSubCrit))
            .SetResultTransformer(new DistinctRootEntityResultTransformer());

Uppdate:
Got some new info and have refactored the sql a bit.
Uppdate: Got a semi working subquery, need to figure out how to set it upp correctly.

  • 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-31T18:20:42+00:00Added an answer on May 31, 2026 at 6:20 pm

    Finally got it working and have updated my criteria code with the final working version in my case. I don’t know exactly how close this is to my original SQL code but the result seams to be the same wich is the most important.

    var invoiceSubCrit = DetachedCriteria.For<Invoice>()
                .Add(Restrictions.Eq("Cancelled", false))
                .Add(Restrictions.EqProperty("OrderReference.ID", "OP.ID"))
                .AddOrder(NhOrder.Desc("Created"))
                .SetProjection(Projections.Property("DueDate"))
                .SetMaxResults(1);
    
            var crit = Session.CreateCriteria<OrderPart>("OP")
                .CreateCriteria("OP.Bookings", "AB", JoinType.InnerJoin)
                .CreateCriteria("OP.Order", "O", JoinType.InnerJoin)
                .CreateCriteria("AB.ActivityPart", "AP", JoinType.InnerJoin)
                .Add(NhExpression.Eq("AP.ID", ActivityID))
                .Add(NhExpression.Eq("OP.Payed", false))
                .Add(NhExpression.Not(NhExpression.Eq("O.OrderStatus", OrderStatus.Cancelled)))
                .Add(NhExpression.Not(NhExpression.Eq("AB.Status", QueueStatus.Reserve)))
                .Add(Subqueries.Gt(DateTime.Now.Date, invoiceSubCrit))
                .SetResultTransformer(new DistinctRootEntityResultTransformer());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to replicate a database from SQL server 2000 to 2005 they
Trying to replicate the following query using Zend_Db_Select . Any pointers? SELECT compounds.id as
I am trying to replicate the following formula from Excel in to a C#
Ok, I am trying to replicate the following SQL query into a Linq expression:
I am trying to replicate the following code using HTML Helper in CakePHP 2.1.
I'm trying to replicate something similar to the following map, where the polygonal area
I am trying to replicate a table produced from a sybase database, I dont
I have been trying to replicate the buffer overflow example3 from this article aleph
I'm trying to replicate the following Ninject syntax in Unity, but not having any
I am trying to replicate the following social bar that is off to the

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.