How do I get row count using the NHibernate QueryOver api?

2,714 views
Skip to first unread message

Jim Geurts

unread,
Mar 22, 2010, 8:04:04 AM3/22/10
to nhusers
HI all,

I'm using the QueryOver api that is part of NHibernate 3.x. I would
like to get a row count, but the method I'm using returns all objects
and then gets the count of the collection. Is there a way to just
return an integer/long value of the number of rows without returning
all of the row data?

I'm currently using:

_session.QueryOver<MyObject>().Future().Count()

if it matters, that call is being used right after one like:

_session.QueryOver<MyObject>().Skip(pageId-1 *
PageSize).Take(PageSize).Future();

Thanks for any help with this,

Jim

Tuna Toksoz

unread,
Mar 22, 2010, 9:54:16 AM3/22/10
to nhu...@googlegroups.com
isn't future for future queries, why do you need it ?

Just execute the query.

Tuna Toksöz
Eternal sunshine of the open source mind.

http://devlicio.us/blogs/tuna_toksoz
http://tunatoksoz.com
http://twitter.com/tehlike





--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.


Richard Brown (gmail)

unread,
Mar 22, 2010, 10:03:03 AM3/22/10
to nhu...@googlegroups.com
I think you can use:
 
.Select(Projections.RowCount())
.List<int>();
 
... or use:
 
.RowCount()
 
Regards,
    Richard

Mohamed Meligy

unread,
Mar 23, 2010, 3:51:50 AM3/23/10
to nhu...@googlegroups.com
I think you just need to make the call (Count) BEFORE the paging code (Take, Skip).

--
Mohamed Meligy
Senior Developer, Team Lead Backup (.Net Technologies - TDG - Applications)
Injazat Data Systems
P.O. Box: 8230 Abu Dhabi, UAE.

Phone:  +971 2 6992700
Direct:   +971 2 4045385
Mobile:  +971 50 2623624, +971 55 2017 621

E-mail: eng.m...@gmail.com
Weblog: http://gurustop.net


Richard Brown (gmail)

unread,
Mar 23, 2010, 4:59:24 AM3/23/10
to nhu...@googlegroups.com
I suspect you'll need both the paged query and the count.
 
See the discussion here:
 
 
There's also a method on IQueryOver called ToRowCountQuery() that will clone the query, remove the paging, and project the rowcount, allowing you to use futures too:
 
myPagedQuery
    .ToRowCountQuery()
    .Future<int>();
 
Sent: Tuesday, March 23, 2010 7:51 AM
Subject: Re: [nhusers] How do I get row count using the NHibernate QueryOver api?

Reply all
Reply to author
Forward
0 new messages