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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:45:20+00:00 2026-05-27T20:45:20+00:00

I have constructed the following query using NHibernate which will give me a collection

  • 0

I have constructed the following query using NHibernate which will give me a collection of MenuView items which contain a given page (referenced by the pages id).

// Only retrieve the required properties from Menu object
ProjectionList menuViewProjections = Projections.ProjectionList()
    .Add(Projections.Property("ID"), "ID")
    .Add(Projections.Property("Name"), "Name")
    .Add(Projections.Property("Description"), "Description");

var menus = session.CreateCriteria(typeof(Menu))
    // Only menu's that are editable
    .Add(Restrictions.Eq("IsEditable", true))

    // Only project required properties
    .SetProjection(menuViewProjections)

    // Only menu's that contain this page (Menu object has IList<Page> property called 'Pages')
    .CreateCriteria("Pages")
    // Restrict to menu's containing the pages with an id of the specified value
    .Add(Restrictions.Eq("ID", pageId))

    // Transform results into required, light-weight, view objects
    .SetResultTransformer(Transformers.AliasToBean(typeof(MenuView)))
    .List<MenuView>();

This works fine; however, now I want to do the opposite: I want to query for all editable menu objects that do not contain the page with specified ID. I have thus far not found a solution for this. I would have thought a simple reversal of the pages section of the above query would suffice resulting in:

// Only retrieve the required properties from Menu object
ProjectionList menuViewProjections = Projections.ProjectionList()
    .Add(Projections.Property("ID"), "ID")
    .Add(Projections.Property("Name"), "Name")
    .Add(Projections.Property("Description"), "Description");

var menus = session.CreateCriteria(typeof(Menu))
    // Only menu's that are editable
    .Add(Restrictions.Eq("IsEditable", true))

    // Only project required properties
    .SetProjection(menuViewProjections)

    // Only retrieve menus that do NOT contain this referenced page
    .CreateCriteria("Pages")
    .Add(Restrictions.Not(Restrictions.Eq("ID", pageId)))

    // Transform results into required view objects
    .SetResultTransformer(Transformers.AliasToBean(typeof(MenuView)))
    .List<MenuView>();

But, this results in the following SQL:

SELECT this_.ID          as y0_,
   this_.Name        as y1_,
   this_.Description as y2_
FROM   [Menu] this_
       inner join PagesInMenu pages3_
         on this_.ID = pages3_.MenuID
       inner join [Page] page1_
         on pages3_.PageID = page1_.ID
WHERE  this_.IsEditable = 1 /* @p0 */
       and not (page1_.ID = 8 /* @p1 */)

Which is still returning results of Menu items that do contain a page with id of 8. Why is this simple reversal of logic not so simple in terms of code?

[Update]
Taking on the suggestions from Firo, the suggested query alteration to;

// Only retrieve menus that do NOT contain this referenced page
.CreateCriteria("Pages")
.Add(Subqueries.PropertyNotIn("Id", querymenuItemswithPage))   <--- query you have would be here

Now generates the following sql statement;

    SELECT this_.ID          as y0_,
       this_.Name        as y1_,
       this_.Description as y2_
FROM   [Menu] this_
       inner join PagesInMenu pages3_
         on this_.ID = pages3_.MenuID
       inner join [Page] page1_
         on pages3_.PageID = page1_.ID
WHERE  this_.IsEditable = 1 /* @p0 */
       and page1_.ID not in (SELECT this_0_.ID as y0_
                             FROM   [Page] this_0_
                             WHERE  this_0_.ID = 1 /* @p1 */

)

Which at first seems exactly what I wanted but sadly (probably due to my poor understanding of joins) is still not returning quite what I wanted. Given the following tables

Menu

Shot of menu table

And then the join-table of PagesInMenu (with a WHERE clause of WHERE PageID = 1)

Show of join table

We can see that page with id of 1 is NOT referenced in menus 5 and 6. I expect the query in question to only return a single row, which would be the ID, Name and Description of Menu with ID of 5 as this is the only menu which page 1 is not included in and which is editable

Instead, the new query returns;

enter image description here

I have crossed out all the rows that are returned but shouldnt be. What is going on here !?

  • 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-27T20:45:21+00:00Added an answer on May 27, 2026 at 8:45 pm

    Update:

    • removed .CreateCriteria("Pages")
    • added subquery

      var querymenuItemswithPage = DetachedCriteria.For<Menu>()
      .CreateCriteria("Pages")
          .Add(Restrictions.Eq("ID", pageId))
      .SetProjection(Projections.Id())
      
      // Only retrieve the required properties from Menu object
      ProjectionList menuViewProjections = Projections.ProjectionList()
          .Add(Projections.Property("ID"), "ID")
          .Add(Projections.Property("Name"), "Name")
          .Add(Projections.Property("Description"), "Description");
      
      var menus = session.CreateCriteria(typeof(Menu))
          // Only menu's that are editable
          .Add(Restrictions.Eq("IsEditable", true))
      
          // Only project required properties
          .SetProjection(menuViewProjections)
      
          // Only retrieve menus that do NOT contain this referenced page
          .Add(Subqueries.PropertyNotIn("Id", querymenuItemswithPage))
      
          // Transform results into required view objects
          .SetResultTransformer(Transformers.AliasToBean(typeof(MenuView)))
          .List<MenuView>();
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following query in ASP.NET/C# code which is failing to return any
I have the following LINQ query that I am using to construct a structure
I have a class which has the following constructor public DelayCompositeDesigner(DelayComposite CompositeObject) { InitializeComponent();
I have in my Form constructor, after the InitializeComponent the following code: using (WebClient
I'm using MySQL and I'm trying to construct a query to do the following:
Based on couple of Api Calls I have constructed the following data structure (Dictionary).
I have the following LINQ query that returns two objects from my database. These
I am using Fluent NHibernate and NHibernate.Core 3 beta 1. I have two classes:
Is it possible to have a generic server-side query like the following? public IQueryable<TContact>
I have the following SQL query - SELECT * FROM dbo.LocalContacts WHERE name LIKE

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.