Nhibernate行号超过分区c#
作者:互联网
我是nhibernate的新人,需要你的帮助.
如何在nhibernate-criteria C#中执行此sql查询
select * from
(
select *, row_number() over (partition by questionaireId order by stepnumber desc) rid
from myTable
) t
where t.rid=1
解决方法:
首先,小注意:选择*将不起作用,所有列必须明确命名.其次,我不确定你想要实现什么目标.如果这应该做某种分页我有更好的建议(数字2)下面).但是,让我们先尝试做你想做的事.
1)因此,考虑到我们应该明确选择列,这是一种如何将SQL语句转换为Criteria语法的方法:
var list = session
.CreateCriteria<MyEntity>() // mapped to 'myTable' table
.SetProjection
(
Projections.SqlProjection
(
"Code, Name, row_number() over (partition by questionaireId order by stepnumber desc) as rid"
, new string[] {"Code", "Name"}
, new IType[] { NHibernate.NHibernateUtil.String
, NHibernate.NHibernateUtil.String }
)
)
.SetMaxResults(1) // this is something like t.rid = 1
.SetResultTransformer(Transformers.AliasToBean<MyEntity>())
.List<MyEntity>()
;
如果您的映射类MyEntity具有属性Code和Name …这将执行select,如下所示:
SELECT TOP 1 Code, Name
, row_number() over (partition by questionaireId order by stepnumber desc) rid
FROM myTable
并且在执行时,结果被限制在属性(代码,名称)中,并且仅选择由您的子句排序的第一行.
2)Native NHibernate分页
因为您已经提到过:我是NHibernate的新手我猜测您正在尝试基于Order By属性进行分页.如果是这种情况,您的解决方案就会简单得多.使用此标准:
var list = session
.CreateCriteria<MyEntity>()
.AddOrder(new Order("stepnumber", false)) // the property to be used for ORDER BY
.Add(... where conditions ala ICriterion
...
.SetFirstResult(100) // skip 100 rows
.SetMaxResults(20) // take 20 rows
.List<Country>();
标签:c,nhibernate,row-number,nhibernate-criteria 来源: https://codeday.me/bug/20190704/1375569.html