MySQL C#查找某个最大距离内的Lat-Lon条目
本文关键字:距离 Lat-Lon 条目 查找 MySQL | 更新日期: 2023-09-27 18:19:53
我正在尝试查找所有具有纬度的数据库条目,这些条目位于距离我所在位置一定英里的范围内。
我的答案是基于这个堆叠式的答案:
MySQL大圆距离(Haversine公式)
这是我所拥有的:
IQueryable<myTable> busLst = (from b in db.myTable
where (3959 * Math.Acos(Math.Cos(radians(latLonRequest.lat)) * Math.Cos(radians(b.lat))
* Math.Cos(radians(b.lon) - radians(latLonRequest.lon)) + Math.Sin(radians(latLonRequest.lat)) *
Math.Sin(radians(b.lat)))) < latLonRequest.MaxDistance
select b
);
我得到以下错误:
"errorCode": "NotSupportedException",
"message": "LINQ to Entities does not recognize the method 'Double Acos(Double)' method, and this method cannot be translated into a store expression.",
"stackTrace": "[GetByLatLonRequest: 6/24/2013 6:57:14 PM]:'n[REQUEST: {lat:3,lon:3,maxDistance:10,measureSystem:N}]'nSystem.NotSupportedException: LINQ to Entities does not recognize the method 'Double Acos(Double)' method, and this method cannot be translated into a store expression.'r'n at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.DefaultTranslator.Translate(ExpressionConverter parent,
这意味着linq不知道如何将函数转换为SQL。你要做的是先提取你的数据,然后在上面运行你的逻辑:
IQueryable<myTable> busLst = (from b in db.myTable.AsEnumerable()
where (3959 * Math.Acos(Math.Cos(radians(latLonRequest.lat)) * Math.Cos(radians(b.lat))
* Math.Cos(radians(b.lon) - radians(latLonRequest.lon)) + Math.Sin(radians(latLonRequest.lat)) *
Math.Sin(radians(b.lat)))) < latLonRequest.MaxDistance
select b
);
这将在运行数学函数之前枚举结果。
您最好构建谓词:http://www.albahari.com/nutshell/predicatebuilder.aspx
编辑:不知怎的以为OP在说TSQL,但没有这样的话。
这个函数似乎是可以在MySQL中定义的,为什么不创建一个SQL函数或存储过程并从那里开始呢?顺便说一句,根据地点的数量,你可能想调查地理/空间数据类型,因为有更好的索引
http://dev.mysql.com/doc/refman/5.0/en/mysql-spatial-datatypes.html