views:

184

answers:

1

I am new to nHibernate. I understand how to use mapping using Fluent nHibernate. Now I would like to use a little more complex query. However I am not sure how I would map or even approach this. Here is what I would like to do in an sql query:

SELET 
  Zone,
  CountOfStyles = (Select Count(1) from anotherTable Where StoreZoneId = zone.ZoneID),
  ZoneId,
  ZoneTitle,
  ZoneDescription
FROM
  Zones

How would I map this using fluent nHibernate?

+1  A: 

I think this might get you started...

UPDATE:

mapping

Map(x => x.CountOfStyles).Formula("(Select Count(1) from anotherTable Where StoreZoneId = ZoneID)");
mxmissile
he wants the count to be part of the query for the entity, not to eager load the list of styles just to get the count.
Darren Kopp
Darren is right. I just need the count of styles.
vikasde
Gotcha, I see. I'm thinking using a View at this point.
mxmissile
I could do that, but the whole idea to use nhibernate was to get away from views/sprocs. There is no other way? My query is not really complex :)
vikasde
Just found this: http://stackoverflow.com/questions/887474/nhibernate-property-formula-filter
mxmissile
And more here... http://ayende.com/Blog/archive/2006/12/26/LocalizingNHibernateContextualParameters.aspx
mxmissile
That seems to work, though how does it grab the zoneId?
vikasde
I got it working. Since both of my fields are called the same in both tables, I did not know exactly how to use the parent field. I just had to add the anotherTable into the where clause. Map(x => x.CountOfStyles).Formula("(Select Count(1) from anotherTable Where anotherTable.StoreZoneId = ZoneID)");
vikasde