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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T12:43:15+00:00 2026-06-16T12:43:15+00:00

I have a small problem with multiple instances of the same object after a

  • 0

I have a small problem with multiple instances of the same object after a join to an other table. For testing I create one Store with two Products (ManyToMany-Relation). The following snippet hopefully describes my problem.

var preResult = _session.QueryOver<Store>().List(); // One store

Product productAlias = null;
var result = _session.QueryOver<Store>()
    .JoinAlias(s => s.Products, () => productAlias)
    .List();                                        // Two instances of the same store

I even think this behavior is correct but how can I prevent the multiple instances? Is it possible within the query?


Just for information why I need to make this unnecessary join: I want to extend the query according to different critirias, similar to this:

Product productAlias = null;
var query = _session.QueryOver<Store>().JoinAlias(s => s.Products, () => productAlias);
if (!string.IsNullOrWhiteSpace(criteria.ProductName))
{
    query.Where(Restrictions.On(() => productAlias.Name).IsInsensitiveLike(criteria.ProductName));
}

if (criteria.ProductType != null)
{
    query.Where(s => productAlias.Type == criteria.ProductType);
}

var result = query.List();

Here I ran into different problems, depending on the criterias.

  • 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-16T12:43:17+00:00Added an answer on June 16, 2026 at 12:43 pm

    Let’s split solution into two queries.

    1. Top one QueryOver<Store>() will be correctly returning just a distinct list. And what’s more, by design it will support paging (Take(), Skip()).
    2. The inner one, will be returning just a list of Store IDs, which fully meet whatever criteria…

    The result SQL will look like this

    SELECT ... // top one 
    FROM Store
    WHERE StoreID IN ( SELECT StoreID ...) // inner one
    

    Inner

    Let’s start with the inner select, the NHibernate detached QueryOver:

    Store storeAlias = null;
    Product productAlias = null;
    
    // detached query, resulting in a set of searched StoreID 
    var subQuery = QueryOver.Of<Store>(() => storeAlias)
        .JoinAlias((s) => s.Products, () => productAlias)
        .Select((s) => s.ID); // ID projection
    
    if (!string.IsNullOrWhiteSpace(criteria.ProductName))
    {
        subQuery.Where(Restrictions.On(() => productAlias.Code)
            .IsInsensitiveLike(criteria.ProductName));
    }
    

    Top

    Once we have filtered the Store we can use this subquery in top one

    var query = session.QueryOver<Store>()
       // IN clause
       .Where(Subqueries.PropertyIn("ID", subQuery.DetachedCriteria))
       .Skip(100) 
       .Take(50) // paging over already distinct resultset
       ;
    
    var result = query.List<Store>();
    

    And now we can apply whatever filter to inner query, and get list of Store IDs which do meet filter criteria… while working with top query, which is distinct…

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

Sidebar

Related Questions

I have a small problem with multiple SurfaceViews in a LinearLayout . How can
Possible Duplicate: Python: Split string with multiple delimiters I have a small syntax problem.
have small problem, and would very much appreciate help :) I should convert byte
I have small problem with Spring MVC. Basically what I'm trying to do is
I have a small problem and I've figured out where when and why it
I have a small problem which i can't seem to figure out. I tried
I have a small problem which i can't seem to solve myself. Look at
I have a small problem with the visibility of an ellipse. In runtime I
I have a small problem with Smarty... I have two different template files in
I have a small problem where I want to find the next active item

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.