SQL 如何选择 nHibernate 子查询结果的 Count(*)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/116188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I select the Count(*) of an nHibernate Subquery's results
提问by ForCripeSake
I need to do the following for the purposes of paging a query in nHibernate:
为了在 nHibernate 中分页查询,我需要执行以下操作:
Select count(*) from
(Select e.ID,e.Name from Object as e where...)
I have tried the following,
我尝试了以下方法,
select count(*) from Object e where e = (Select distinct e.ID,e.Name from ...)
and I get an nHibernate Exception saying I cannot convert Object to int32.
我收到一个 nHibernate 异常,说我无法将 Object 转换为 int32。
Any ideas on the required syntax?
关于所需语法的任何想法?
EDIT
编辑
The Subquery uses a distinct clause, I cannot replace the e.ID,e.Name with Count(*)
because Count(*) distinct
is not a valid syntax, and distinct count(*)
is meaningless.
子查询使用不同的子句,我不能用 e.ID,e.Name 替换,Count(*)
因为Count(*) distinct
它不是有效的语法,而且distinct count(*)
毫无意义。
回答by bipinkarms
NHibernate 3.0 allows Linq query.
NHibernate 3.0 允许 Linq 查询。
Try this
尝试这个
int count = session.QueryOver<Orders>().RowCount();
回答by Matt Hinze
var session = GetSession();
var criteria = session.CreateCriteria(typeof(Order))
.Add(Restrictions.Eq("Product", product))
.SetProjection(Projections.CountDistinct("Price"));
return (int) criteria.UniqueResult();
回答by ForCripeSake
Solved My own question by modifying Geir-Tore's answer.....
通过修改 Geir-Tore 的回答解决了我自己的问题......
IList results = session.CreateMultiQuery()
.Add(session.CreateQuery("from Orders o").SetFirstResult(pageindex).SetMaxResults(pagesize))
.Add(session.CreateQuery("select count(distinct e.Id) from Orders o where..."))
.List();
return results;
回答by Geir-Tore Lindsve
Here is a draft of how I do it:
这是我如何做的草稿:
Query:
询问:
public IList GetOrders(int pageindex, int pagesize)
{
IList results = session.CreateMultiQuery()
.Add(session.CreateQuery("from Orders o").SetFirstResult(pageindex).SetMaxResults(pagesize))
.Add(session.CreateQuery("select count(*) from Orders o"))
.List();
return results;
}
ObjectDataSource:
对象数据源:
[DataObjectMethod(DataObjectMethodType.Select)]
public DataTable GetOrders(int startRowIndex, int maximumRows)
{
IList result = dao.GetOrders(startRowIndex, maximumRows);
_count = Convert.ToInt32(((IList)result[1])[0]);
return DataTableFromIList((IList)result[0]); //Basically creates a DataTable from the IList of Orders
}
回答by user8456
If you just need e.Id
,e.Name
:
如果您只需要e.Id
,e.Name
:
select count(*) from Object where
.....
select count(*) from Object where
.....
回答by Marcelo Salazar
I prefer,
我更喜欢,
public IList GetOrders(int pageindex, int pagesize, out int total)
{
var results = session.CreateQuery().Add(session.CreateQuery("from Orders o").SetFirstResult(pageindex).SetMaxResults(pagesize));
var wCriteriaCount = (ICriteria)results.Clone());
wCriteriaCount.SetProjection(Projections.RowCount());
total = Convert.ToInt32(wCriteriaCount.UniqueResult());
return results.List();
}