我正在尝试查找所有数据库条目,这些条目的纬度落入我所在位置的某个英里范围内。
我的答案是基于这个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,
答案 0 :(得分:1)
这意味着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
答案 1 :(得分:1)
编辑:不知何故认为OP正在谈论TSQL,但没有这样的谈话。
该函数似乎可以在MySQL中定义,为什么不创建SQL函数或存储过程并从那里开始?顺便说一下,根据位置的数量,您可能想要调查地理/空间数据类型,因为它们有更好的索引
http://dev.mysql.com/doc/refman/5.0/en/mysql-spatial-datatypes.html