2013-01-31 36 views
1

我在我的数据库中有一个包含地理位置的表格。我想编写一个函数来查找最接近点的位置。我在NHibernate试过这个:按NHibernate中的函数结果排序?

public Location GetClosestLocation(double latitude, double longitude) 
{ 
    var closest = Session.QueryOver<Location>() 
     .OrderBy(location => 
      (location.Latitude - latitude) + 
      (location.Longitude - longitude)) 
     .Asc.SingleOrDefault(); 

    return closest; 
} 

但它不起作用 - 我得到一个运行时错误。

我能做些什么来返回最近的位置?是否有可能通过NHibernate的简单函数的结果进行排序?

回答

3

我不认为QueryOver可以理解这种表达式 - 它使用lambda表达式仅限于标识属性。 OrderBy()的重载需要IProjection,它提供了更多的灵活性。

使用LINQ,你应该能够使用几乎相同的代码,你试过:

var closest = Session.Query<Location>() 
        .OrderBy(location => 
           (location.Latitude - latitude) + 
           (location.Longitude - longitude)) 
        .SingleOrDefault(); 
+1

谢谢,我想是这样的。给任何其他发现这个问题的人留下一个提示 - 记住拿走绝对的距离! – Oliver