2

I am stuck in a problem while creating a query in nhibernate in c#. Actually I have to create a criteria query for following sql statement

select fCompanyID,ROW_NUMBER() over( PARTITION BY fCompanyID order by fPropertyID)
from  tARCustomer  

But it seems that nhibernate does't support Row_Number() partition by as till now I googled. I need this query functionality in nhibernate but I am not getting how to do it. If anyone did it befor then please let me know / help me to solve out this problem.

Thanks,

Awadhendra
  • 355
  • 2
  • 10
  • 34
  • check this post http://stackoverflow.com/questions/12897943/select-every-nth-row-with-nhibernate. I think this kind of RDBMS dependent instructions are not supported by NHibernate. – Agustin Meriles Jan 25 '13 at 18:29

1 Answers1

3

Well, NHibernate has solution for anything. This could be the Criteria syntax:

var list = session
  .CreateCriteria<Customer>()
  .SetProjection
  (
       Projections.SqlProjection
       (   
          "fCompanyID, ROW_NUMBER() over( PARTITION BY fCompanyID order by fPropertyID) as RowNumber"
          , new string[] {"fCompanyID", "RowNumber"}
          , new IType[] { NHibernate.NHibernateUtil.Int32, NHibernate.NHibernateUtil.Int32}
       )
  )
  .SetResultTransformer(Transformers.AliasToBean<ResultDTO>())
  .List<ResultDTO>()
;

And the ResultDTO like this:

public class ResultDTO
{
  public virtual int RowNumber { get; set; }
  public virtual int fCompanyID{ get; set; }
}

And now you can work with a list as a set of ResultDTO

Radim Köhler
  • 122,561
  • 47
  • 239
  • 335