我需要知道如何用NHibernate的ICriteria格式重写下面的SQL查询。它基本上是一种模仿MS-SQL的RANK()特性并只返回最新结果的方法。
SELECT a.Name, a.Value, a.CreationDate
FROM MyTable a
WHERE EXISTS
(
SELECT NULL
FROM
(
SELECT TOP 1 CreationDate
FROM MyTable
WHERE Name = a.Name
ORDER BY CreationDate DESC
) b
WHERE b.CreationDate = a.CreationDate
)例如,给定一个包含以下数据的表:
名称、值、创建
‘'Key One','value one v1','2009-11-11’
‘'Key One','value one v2','2009-11-12’
‘密钥二’,‘值二v1','2009-11-09’
‘密钥三’,‘值三v2','2009-09-09’
‘密钥三’,‘值三v1','2009-09-06’
‘密钥三’,‘值三v3','2009-10-01’
上述查询的结果为:
‘'Key One','value one v2','2009-11-12’
‘密钥二’,‘值二v1','2009-11-09’
‘'Key’,'value three v3,'2009-10-01‘
发布于 2009-11-12 15:13:32
当查询复杂时,不要使用"Criteria API“而使用"HQL",使用"Criteria API”可以浪费很多时间来找到正确的解决方案。您所显示查询几乎无需更改即可使用。看看这个:https://www.hibernate.org/hib_docs/nhibernate/html/queryhql.html
最后,生成的代码通常与"Criteria API“或"HQL”相同……
一段代码:
StringBuilder query = new StringBuilder();
query.Append("from MyTable where ");
query.Append("DateOfDayStart <= :startDate and DateOfDayEnd >= :endDate ");
IList<MyTable> list = session.CreateQuery(query.ToString())
.SetDateTime("startDate", startDate)
.SetDateTime("endDate", endDate)
.List<MyTable>();
return list;https://stackoverflow.com/questions/1719277
复制相似问题