【问题标题】:Using linq to find out the nearest data with the given latitude and longitude使用 linq 查找具有给定纬度和经度的最近数据
【发布时间】:2018-08-17 04:15:01
【问题描述】:

我有一个数据表,其中存储了按纬度和经度存储的数据。

我需要使用 LINQ 查询来过滤这个数据表,并从给定的纬度和经度中找到最近的位置。

例如: 28.9873 -- 纬度, 80.1652 -- 经度

【问题讨论】:

  • 您应该(最好的方法 imo)在您的数据库中使用地理数据类型并让数据库为您排序,否则我将创建一个存储过程,根据最小值提供最近的位置Lat 和 Lng 的差值之和。
  • @mjwills 将其存储在 SQL Server 中。

标签: c# sql-server linq sqlgeography


【解决方案1】:

试试这个:

var minDistance = collection.Min( x => Math.Sqrt( Math.Pow( wantedLatitude - x.Latitude ) + Math.Pow( wantedLongitude - x.Longitude )));

var closestLocation = collection.First( x => Math.Sqrt( Math.Pow( wantedLatitude - x.Latitude ) + Math.Pow(wantedLongitude - x.Longitude )) == minDistance );

它经过两次收集,所以我建议通过在第一次运行时找到 Min 元素来优化它,你可以很容易地在 SO 上的其他问题中找到如何做到这一点,并封装到自己方法的距离计算,但是你明白了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多