Search code examples
nhibernateactiverecordcastle-monorail

MonoRail ActiveRecord/NHibernate calculation in where clause


I'm trying to get businesses within a certain number of miles of a user, using a formula to get the distance between the lat/long of the business and the lat/long of the user. Here is the code:

var criteria = DetachedCriteria.For<Core.Models.Business>();      criteria.Add(Restrictions.Le(String.Format(@"(3959*acos(cos(radians({0}))*cos(radians(Latitude))*cos(radians(Longitude)-radians({1}))
                    +sin(radians({0}))*sin(radians(Latitude))))", coordinates.Latitude, coordinates.Longitude), radiusInMiles));

The problem is that ActiveRecord/NHibernate's Restrictions.Le method expects a property name for the first parameter so I can't put a formula in there. How would I do something like this?

Thanks! Justin


Solution

  • Try using Expression.Sql. A couple of examples: