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), radiusI开发者_如何学JAVAnMiles));
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
Try using Expression.Sql
. A couple of examples:
- NHibernate Query that simulates SQL Replace Function
- NHibernate and MySql Keywords
精彩评论