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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:32:06+00:00 2026-05-24T11:32:06+00:00

I have an object model that looks like this (pseudo code): class Product {

  • 0

I have an object model that looks like this (pseudo code):

class Product {
    public ISet<Product> Recommendations {get; set;}
    public ISet<Product> Recommenders {get; set;}
    public ISet<Image> Images {get; set; }
}

When I load a given product and want to display the images of its recommendations, I run into an N+1 problem. (The recommendations are lazy-loaded, then a loop calls the .Images property of each one.)

Product -> Recommendations -> Images

What I want to do is eagerly load this particular part of the graph, but I can’t figure out how to do it. I can load the recommendations eagerly, but not their images. This is what I have been trying, but it doesn’t seem to work:

//get the IDs of the products that will be in the recommendations collection
var recommendedIDs = QueryOver.Of<Product>()
    .Inner.JoinQueryOver<Product>(p => p.Recommenders)
    .Where(r => r.Id == ID /*product we are currently loading*/)
    .Select(p => p.Id);

//products that are in the recommendations collection should load their 
//images eagerly
CurrentSession.QueryOver<Product>()
    .Fetch(p => p.Images).Eager
    .Where(Subqueries.WhereProperty<Product>(p => p.Id).In(recommendedIDs))
    .Future<Product>();

//load the current product
return CurrentSession.QueryOver<Product>()
    .Where(p => p.Id == ID);

Using QueryOver, what is the best way to accomplish this? I don’t want to eagerly load images all the time, just in this particular scenario.


EDIT: I have changed my approach, and while it’s not exactly what I had in mind, it does avoid the N+1 problem. I am now using two queries, one for the product, and one for the images of it’s recommendations. The product query is straight-forward; here is the image query:

//get the recommended product IDs; these will be used in
//a subquery for the images
var recommendedIDs = QueryOver.Of<Product>()
    .Inner.JoinQueryOver<Product>(p => p.Recommenders)
    .Where(r => r.Id == RecommendingProductID)
    .Select(p => p.Id);

//get the logo images for the recommended products and
//create a flattened object for the data
var recommendations = CurrentSession.QueryOver<Image>()
    .Fetch(i => i.Product).Eager
    /* filter the images down to only logos */
    .Where(i => i.Kind == ImageKind.Logo)
    .JoinQueryOver(i => i.Product)
    /* filter the products down to only recommendations */
    .Where(Subqueries.WhereProperty<Product>(p => p.Id).In(recommendedIDs))
    .List().Select(i => new ProductRecommendation {
        Description = i.Product.Description,
        ID = i.Product.Id,
        Name = i.Product.Name,
        ThumbnailPath = i.ThumbnailFile
    }).ToList();

return recommendations;
  • 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-24T11:32:08+00:00Added an answer on May 24, 2026 at 11:32 am

    JoinAlias is another way to eagerly fetch related records, plus we can use it to dig another level deeper through Recommendations down to Images. We’ll use LeftOuterJoin because we want to load the product even if it has no recommendations.

    Product recommendationAlias = null;
    Image imageAlias = null;
    
    return CurrentSession.QueryOver<Product>()
        .JoinAlias(x => x.Recommendations, () => recommendationAlias, JoinType.LeftOuterJoin)
        .JoinAlias(() => recommendationAlias.Images, () => imageAlias, JoinType.LeftOuterJoin)
        .Where(x => x.Id == ID)
        .TransformUsing(Transformers.DistinctRootEntity)
        .SingleOrDefault();
    

    When discussing eager fetching of multiple collections with NHibernate, you often hear people mention Cartesian products, but that’s not a concern here. If however, you wished to load the following graph instead…

     Product -> Recommendations -> Images
             -> Images
    

    … then Product.Recommendations.Images X Product.Images would form a Cartesian product that we should avoid. We could do so like this:

    Product recommendationAlias = null;
    Image imageAlias = null;
    
    var productFuture = CurrentSession.QueryOver<Product>()
        .JoinAlias(x => x.Recommendations, () => recommendationAlias, JoinType.LeftOuterJoin)
        .JoinAlias(() => recommendationAlias.Images, () => imageAlias, JoinType.LeftOuterJoin)
        .Where(x => x.Id == ID)
        .TransformUsing(Transformers.DistinctRootEntity)
        .FutureValue();
    
    var imagesFuture = CurrentSession.QueryOver<Product>()
        .Fetch(x => x.Images).Eager
        .Where(x => x.Id == ID)
        .TransformUsing(Transformers.DistinctRootEntity)
        .Future();
    
    return productFuture.Value;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an object that looks like this: class Model { public string Category
I have an object model that looks like this: public class MyObjectModel { public
I have a model that looks something like this: public class SampleModel { public
I have written a custom server control which (pseudo-code) looks like public class MyCustomCtrl
I have a controller method that looks like this: [AcceptGet] public ActionResult Index(SecurityMatrixIndexViewModel model)
I have a model that looks something like this: class Comment < ActiveRecord::Base ...
I have a Django model that looks something like this: class Person(models.Model): name =
I have a ViewModel that looks like this: public class CreateReviewViewModel { public string
I have a simple question. I have a model that looks like this: public
I have an object that looks like this when outputted via a print_r Array

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.