Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I select the Count(*) of an nHibernate Subquery's results

I need to do the following for the purposes of paging a query in 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.

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.

like image 429
ForCripeSake Avatar asked Sep 22 '08 17:09

ForCripeSake


2 Answers

var session = GetSession();
var criteria = session.CreateCriteria(typeof(Order))
                    .Add(Restrictions.Eq("Product", product))
                    .SetProjection(Projections.CountDistinct("Price"));
return (int) criteria.UniqueResult();
like image 51
Matt Hinze Avatar answered Oct 07 '22 18:10

Matt Hinze


NHibernate 3.0 allows Linq query.

Try this

int count = session.QueryOver<Orders>().RowCount();
like image 20
bipinkarms Avatar answered Oct 07 '22 20:10

bipinkarms