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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:11:48+00:00 2026-05-11T18:11:48+00:00

I have entity Recipes and it has a HasMany collection comments. In a MVC

  • 0

I have entity Recipes and it has a HasMany collection comments.

In a MVC controller action I am getting the recipe
and then displaying the comments.

How do I sort the comments in descending order of “EnteredOn”?

Where do I sort them?

  Recipe recipe = session.Load<Recipe>(id);
    NHibernateUtil.Initialize(recipe.Comments);

Malcolm

  • 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-11T18:11:48+00:00Added an answer on May 11, 2026 at 6:11 pm

    I think I would just retrieve the comments as-is (not particulary sorted), and then just sort the Comments collection of the recipe, before you display the comments.

    Depending on how you’ve created your class, and your mapping, I think this is the only way since a set and bag mapping, represent un-ordered collections in NHibernate.

    Something like this:

    Recipe recipe = session.Get<Recipe> (id);
    
    var orderedComments = recipe.Comments.OrderBy ( comment => comment.EnteredOn );
    
    foreach( Comment c in orderedComments )
    {
       // display the comment
    }
    

    Where my Reciple entity looks something like this:

    public class Recipe
    {
       // ...
       ...
    
       private ISet<Comment> _comments = new HashedSet<Comment>();
    
       public ReadOnlyCollection<Comment> Comments
       {
          get { return _comments.ToList().AsReadOnly(); }
       }
    
       public void AddComment( Comment c )
       {
           if( c != null && !_comments.Contains (c) )
           {
              c.Recipe = this;
              _comments.Add (c);
           }
       }
    
       public void RemoveComment(Comment c )
       {
           if( c != null && _comments.Contains (c) )
           {
               c.Recipe = null;
               _comments.Remove(c);
           }
       }
    }
    

    and the mapping:

    <class name="Recipe" table="Recipes">
        ...
        <set name="Comments" access="field.camelcase-underscore" ... >
            ...
        </set>
    </class>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have entity Ideas, which has child entity collection ChildIdeas. I need to load
In my business model I have Entity class (IPoint interface ) that has a
I have an entity class that has a property with an underlying db column
I have an entity that has a state table associated with it. The state
Let say I have an entity user which has a one to many relationship
i have entity A that has foreign key to entity B, does entity B
suppose i have @Entity A, which has an eager @ManyToOne to @Entity B, and
I have entity that has a version. It has composite primary key where one
I have entity called Customer and it has three properties: public class Customer {
I have a Recipe entity that contains two images: @PersistenceCapable(identityType = IdentityType.APPLICATION, detachable=true) public

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.