用Linq按顺序进行数学计算

用Linq按顺序进行数学计算,linq,linq-to-entities,Linq,Linq To Entities,我对Linq是个新手,有点麻烦 这是我的搜索存储库: public static List<Centre> Search(Search search) { using (var context = new MeetingRoomsContext()) { var query = context.Centres .Include("Geo") .OrderBy( NEED HELP WI

我对Linq是个新手,有点麻烦

这是我的搜索存储库:

 public static List<Centre> Search(Search search)
 {
    using (var context = new MeetingRoomsContext())
    {
        var query = context.Centres
                .Include("Geo")
                .OrderBy( NEED HELP WITH THIS PART )

        return query.ToList();
    }
 }
我需要打破这一点,并对数据库中的坐标做一些数学运算,以便按最接近搜索点的顺序对结果进行排序

用数学的术语来说,这就是毕达哥拉斯:

squareRootOf((difference in latitude * difference in latitude) + 
             (difference in longitude * difference in longitude))

我真的不知道该怎么做。非常感谢任何帮助

如何实现您自己的计算毕达哥拉斯平方根的IComparer?然后OrderBy将自动比较这一点。

参见示例。

根本不需要平方根;按距离平方排序与按距离排序相同:

.OrderBy(x => (x.Latitude - target.Latitude)*(x.Latitude - target.Latitude)
              + (x.Longitude - target.Longitude)*(x.Longitude - target.Longitude))

这种技巧通常用于碰撞检测(如视频游戏),以避免计算许多平方根。

在Sql Server 2008中,有一种空间索引功能,可用于此类场景,但我认为EF不支持它。
.OrderBy(x => (x.Latitude - target.Latitude)*(x.Latitude - target.Latitude)
              + (x.Longitude - target.Longitude)*(x.Longitude - target.Longitude))