Getting Row Count in NHibernate
Getting the row count of a table, that is easy in NHibernate. You can do it this way:
Code
ICriteria criteria = CurrentSession.CreateCriteria<Member>(); | |
criteria.Add(Expression.Eq("Code",code)); | |
return (int) criteria.List().Count; |
But this is very heavy weighted. You are retrieving a whole list of objects!!!! But you just need to know the numbers of objects! You are wasting a lot of processing power!
You can do it this way. That is only getting a row count, just a number! Therefore, a "select count(*)" query is executed.
Code
ICriteria criteria = CurrentSession.CreateCriteria<Member>(); | |
criteria.Add(Expression.Eq("Code",code)); | |
criteria.SetProjection(Projections.RowCount()); | |
return (int) criteria.UniqueResult(); |
Trackback address for this post
Trackback URL (right click and copy shortcut/link location)
Feedback awaiting moderation
This post has 2274 feedbacks awaiting moderation...
Form is loading...