NHibernate排序a有许多集合

NHibernate排序a有许多集合,nhibernate,Nhibernate,我有实体食谱,它有许多收藏评论 在MVC控制器操作中,我得到了配方 然后显示注释 如何按“EnteredOn”降序排列注释 我在哪里分类 Recipe recipe = session.Load<Recipe>(id); NHibernateUtil.Initialize(recipe.Comments); Recipe=session.Load(id); 初始化(recipe.Comments); Malcolm我想我只需要按原样检索注释(不需要特别排序),然后在显

我有实体食谱,它有许多收藏评论

在MVC控制器操作中,我得到了配方 然后显示注释

如何按“EnteredOn”降序排列注释

我在哪里分类

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

Malcolm

我想我只需要按原样检索注释(不需要特别排序),然后在显示注释之前对配方的注释集合进行排序

根据您创建类和映射的方式,我认为这是唯一的方法,因为集合和包映射在NHibernate中表示未排序的集合

大概是这样的:

Recipe recipe = session.Get<Recipe> (id);

var orderedComments = recipe.Comments.OrderBy ( comment => comment.EnteredOn );

foreach( Comment c in orderedComments )
{
   // display the comment
}
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);
       }
   }
}
Recipe=session.Get(id);
var orderedComments=recipe.Comments.OrderBy(comment=>comment.EnteredOn);
foreach(orderedComments中的注释c)
{
//显示注释
}
我的Reciple实体如下所示:

Recipe recipe = session.Get<Recipe> (id);

var orderedComments = recipe.Comments.OrderBy ( comment => comment.EnteredOn );

foreach( Comment c in orderedComments )
{
   // display the comment
}
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);
       }
   }
}
公共类配方
{
// ...
...
私有ISet_comments=new HashedSet();
公共只读集合注释
{
获取{return}comments.ToList().AsReadOnly();}
}
公共无效添加注释(注释c)
{
如果(c!=null&&!\u comments.Contains(c))
{
c、 配方=这个;
_添加(c);
}
}
公共无效删除通知(注释c)
{
如果(c!=null&&u comments.Contains(c))
{
c、 配方=空;
_删除(c)项;
}
}
}
以及映射:

<class name="Recipe" table="Recipes">
    ...
    <set name="Comments" access="field.camelcase-underscore" ... >
        ...
    </set>
</class>

...
...