views:

26

answers:

1

Hey,

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

A: 

Try using Expression.Sql. A couple of examples:

Mauricio Scheffer
Thanks, unfortunately Restrictions.Sql doesn't exist, perhaps that was just in older versions?
Justin
@Justin: sorry, it's actually called Expression.Sql
Mauricio Scheffer
That worked, thanks! Now I can remove the nasty dynamic sql...
Justin