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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:51:00+00:00 2026-05-14T21:51:00+00:00

I have a root object that has a property that is a collection. For

  • 0

I have a root object that has a property that is a collection.

For example:

I have a Shelf object that has Books.

// Now
public class Shelf 
{
    public ICollection<Book> Books {get; set;}
}

// Want 
public class Shelf 
{
   public IQueryable<Book> Books {get;set;}
}

What I want to accomplish is to return a collection that is IQueryable so that I can run paging and filtering off of the collection directly from the the parent.

var shelf = shelfRepository.Get(1);

var filtered = from book in shelf.Books
               where book.Name == "The Great Gatsby"
               select book;

I want to have that query executed specifically by NHibernate and not a get all to load a whole collection and then parse it in memory (which is what currently happens when I use ICollection).

The reasoning behind this is that my collection could be huge, tens of thousands of records, and a get all query could bash my database.

I would like to do this implicitly so that when NHibernate sees an IQueryable on my class it knows what to do.

I have looked at NHibernate’s LINQ provider and currently I am making the decision to take large collections and split them into their own repository so that I can make explicit calls for filtering and paging.

LINQ To SQL offers something similar to what I’m talking about.

  • 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-14T21:51:00+00:00Added an answer on May 14, 2026 at 9:51 pm

    I’ve been trying to come up with a solution for a similar problem.

    You can filter collections off an entity using ISession.FilterCollection. This creates an additional IQuery where you can count, page, add criteria, etc.

    So, for example (my query in FilterCollection may be a little off, but you should get the idea):

    ISession session = GetSession();
    var shelf = session.Get<Shelf>(id);
    var books = session.FilterCollection(shelf.Books, "where Name = :title").SetString("title", "The Great Gatsby").List<Book>();
    

    There are a problem with that, however:

    1. The consumer executing the code
      needs to access
      ISession.CreateFilter, or you need
      to create a method on your
      repository that takes in a property,
      a query, and your query arguments
      (as well as any paging or other
      information). Not really the sexiest
      thing on the planet.
    2. It’s not the LINQ you wanted.

    Unfortunately, I don’t think there’s any way to get what you want out of the box with NHibernate. You could fake it, if you wanted to try, but they seem to fall flat to me:

    Add a method or property that under the covers returns a LINQ to NHibernate IQueryable for this shelf:

    public IQueryable<Book> FindBooks() {
      return Resolver.Get<ISession>().Linq<Book>().Where(b => b.Shelf == this);
    }
    

    where someone might consume that like this:

    var shelf = ShelfRepo.Get(id);
    var books = (from book shelf.FindBooks()
                 where book.Title == "The Great Gatsby"
                 select book);
    

    Yuck! You are bleeding your persistence needs through your domain model! Maybe you could make it a little less worse by having a repository emit IQueryable, which at runtime is actually LINQ to NHibernate:

    public IQueryable<Book> FindBooks() {
      return Resolver.Get<IRepository<Book>>().CreateQuery().Where(b => b.Shelf == this);
    }
    

    Still pretty blah.

    Create your own custom collection type (and potentially an IQueryable implementation) that wraps a private field of the actual books, and map NHibernate to that field. However, it may be a difficult undertaking getting that working with ISession.CreateFilter. You have to consider “discovering” the current session, converting the LINQ expression into something you can use in CreateFilter, etc. Plus, your business logic is still dependent on NHibernate.

    Nothing really satisfies at this point. Until NHibernate can do LINQ over a collection for you, it appears that you’re better off just querying your Book repository normally as has already been suggested, even if it doesn’t seem as sexy or optimal.

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

Sidebar

Related Questions

We have a Java program run as root on Unix, that therefore can read
I have many sites that use the same root category of the Main Site.
I have created an application that writes some data to the root folder of
I am using a machine on which I do not have root access and
Currently I have a root-level repository set up for each project, like so: Project1
I have a text file in the root of my web app http://localhost/foo.txt and
I have a content management application in the root of my website, and I'm
I have long since forgotten the password for the root user on one of
I have a site with the following robots.txt in the root: User-agent: * Disabled:
I have a maven project with the following packages (for illustration only): Root: src/main/java

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.