Friday 12 February 2010

nHibernate Projections in VB.net

Well, this has lead me a merry dance, I can assure you. You'd think it was pretty straightforward to do something as simple as a row count in nHibernate, wouldn't you? Well, in point of fact it actually is pretty simple but it's just not well documented anywhere. Particularly perplexing is the fact that most of the examples seem to have been ripped straight from the Java hibernate implementation and I'm not entirely sure this will even port to C# and work, let alone translate to VB (I haven't tried in C#).

Anyway, just in case some poor soul has the same problem and gets stuck, this is how you do it:


Dim pResults As IList = session.CreateCriteria(GetType(dto.Product)) _
.SetProjection(Projections.RowCount()).List()


Of course "dto.Product" is a custom class I'm using that's specific to what I'm trying to do, so stick your own in there and you should be good to go.

No comments:

Post a Comment