开发者

MonoRail ActiveRecord/NHibernate calculation in where clause

开发者 https://www.devze.com 2023-01-05 23:23 出处:网络
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:

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
0

精彩评论

暂无评论...
验证码 换一张
取 消