c# - MySQL C# 查找某个最大距离内的 Lat Lon 条目

标签 c# mysql servicestack

我正在尝试查找所有具有经纬度且距我的位置在一定英里范围内的数据库条目。

我的答案基于这个 stackoverflow 答案:

MySQL Great Circle Distance (Haversine formula)

这是我所拥有的:

                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

关于c# - MySQL C# 查找某个最大距离内的 Lat Lon 条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17283208/

相关文章:

带组的Mysql查询

caching - 我是否需要使用RedisLocks(ServiceStack.Redis)

c# - 服务栈elmah实现

c# - 访客屏幕分辨率

c# - 新站点的 MVC 或 Webform 架构

c# - 替换位图/图像中特定颜色的有效方法

java - Hibernate 模板中的动态表创建

php - 通过 php/mysql 从表中读取变量

c# - 如何访问 IWebHostBuilder 扩展中的配置

c# - 带有临时字段的 ServiceStack Ormlite 类