我正在尝试查找所有具有经纬度且距我的位置在一定英里范围内的数据库条目。
我的答案基于这个 stackoverflow 答案:
这是我所拥有的:
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/