Search code examples
c#nhibernatenhibernate-criteria

How to do sorting in Nhibernate


I have a Nhibernate criteria on which I want to use sorting orderby asc.Below is my Nhibernate Criteria.

 IList<EmployeeDetails> results;
 results = BmDaoSession.CreateSQLQuery("exec Proc_ABC @Param1=" + batchNo + ", @Param2 ='" + flag + "', @Param3 ='" + backOfficeCompanyCode + "', @Param4 ='" + recordOption + "'")
    .SetResultTransformer(NHibernate.Transform.Transformers.AliasToBean(typeof(EmployeeDetails))).List<EmployeeDetails>();
return results;

Please suggest.


Solution

  • you can use linq to sort it, its OrderBy will do the job for you, by default it does ascending:

    using System.Linq;
    
    IList<Foo> list = new List<Foo>();
    IEnumerable<Foo> sortedEnum = list.OrderBy(f=>f.Bar);
    IList<Foo> sortedList = sortedEnum.ToList();
    

    for Descending:

    IEnumerable<Foo> sortedEnum = list.OrderByDescending(f=>f.Bar);
    IList<Foo> sortedList = sortedEnum.ToList();