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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:38:51+00:00 2026-06-09T05:38:51+00:00

To explain my problem more easily I will create the following fictional example, illustrating

  • 0

To explain my problem more easily I will create the following fictional example, illustrating a very basic many-to-many relationship. A Car can have many Parts, and a Part can belong to many Cars.

DB SCHEMA:

CAR_TABLE
---------
CarId
ModelName

CAR_PARTS_TABLE
---------------
CarId
PartId

PARTS_TABLE
-----------
PartId
PartName

CLASSES:

public class Car 
{
  public int CarId {get;set;}
  public string Name {get;set;}
  public IEnumerable<Part> Parts {get;set;}
}

public class Part 
{
  public int PartId {get;set;}
  public string Name {get;set}
}

Using this very simple model I would like to get any cars that have all the parts assigned to them from a list of parts I am searching on.

So say I have an array of PartIds:

var partIds = new [] { 1, 3, 10};

I want to mimic the following c# code in terms of a database call:

var allCars = /* code to retrieve all cars */

var results = new List<Car>();

foreach (var car in allCars) 
{
  var containsAllParts = true;

  foreach (var carPart in car.Parts)
  {
    if (false == partIds.Contains(carPart.PartId))
    {
      containsAllParts = false;
      break;
    }
  }

  if (containsAllParts)
  {
    results.Add(car);
  }
}

return results;

To be clear: I want to get the Cars that have ALL of the Parts specified from the partIds array.

I have the following query, which is REALLY inefficient as it creates a subquery for each id within the partIds array and then does an IsIn query on each of their results. I am desperate to find a much more efficient manner to execute this query.

Car carAlias = null;
Part partAlias = null;

var searchCriteria = session.QueryOver<Car>(() => carAlias);

foreach (var partId in partIds)
{
  var carsWithPartCriteria = QueryOver.Of<Car>(() => carAlias)
    .JoinAlias(() => carAlias.Parts, () => partAlias)
    .Where(() => partAlias.PartId == partId)
    .Select(Projections.Distinct(Projections.Id()));

  searchCriteria = searchCriteria
    .And(Subqueries.WhereProperty(() => carAlias.Id).In(carsWithPartCriteria));
}

var results = searchCriteria.List<Car>();

Is there a decent way to execute this sort of query using NHibernate?

  • 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-06-09T05:38:52+00:00Added an answer on June 9, 2026 at 5:38 am

    This should be exactly what you want…

    Part part =  null;
    Car car = null;
    var qoParts = QueryOver.Of<Part>(() => part)
                    .WhereRestrictionOn(x => x.PartId).IsIn(partIds)
                    .JoinQueryOver(x => x.Cars, () => car)
                    .Where(Restrictions.Eq(Projections.Count(() => car.CarId), partIds.Length))
                    .Select(Projections.Group(() => car.CarId));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It will be more easy for me to explain the problem if I just
[EDIT] I am changing this to more concisely explain what my problem was, after
The best way to explain my problem is by a example. I have a
I am not entirely sure how to explain my problem but I will try.
I will try to explain the problem that I have with this code. This
I still have following problem EXPLAIN EXTENDED SELECT `item`.`id`, `item`.`timestamp`, `item`.`label` FROM item WHERE
I'd better use the following sample codes to explain my problem: while True: NewThread
I will try to explain this in a different way so it makes more
I will explain my problem. I have a select with multiple options, and one
i will try to explain the problem. I have this structure: offers -------------- id_offer|offer|company

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.