SQL查询在LINQ2SQL中基于距lat / lng的距离提取位置? [英] SQL query to pull locations based on distance from lat/lng, in LINQ2SQL?

查看:159
本文介绍了SQL查询在LINQ2SQL中基于距lat / lng的距离提取位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想针对MS SQL 2008 Server执行查询(从指定的lat / lng拉出给定距离内的位置),无geo字段类型:

<$ (弧度(37))* cos(弧度(lat))* cos(弧度(lng) - 弧度(-122))+ sin(p)弧度(37))* sin(弧度(lat))))AS距距标记距离< 25 ORDER BY距离LIMIT 0,20;

这是这里描述的haversine公式。我想调用查询,可能设置为一个存储过程,使用LINQ - 拉到一个IEnumerable列表中。

该表基本上是这样的:

>

地点表




  • >
  • 名称 - nvarchar ...

  • Lat

  • Lng ...



我不熟悉MS SQL中的数学函数,查询示例是MySQL。这个查询将移植到MS SQL吗?我是否问过试图做到这一点的问题?

解决方案

您发布的查询应该工作得很好。



但是,如果您处于可以更改数据模式的位置,那么SQL Server具有一些本地地理类型 - 如果您可以使用这些类型,则会有内置函数并构建在索引中,这将允许您更高效地执行此类查询 - 请参阅 http://www.microsoft.com/sqlserver/2008/en/us/spatial-data.aspx


I want to perform a query (to pull locations within a given distance from a specified lat/lng) against a MS SQL 2008 Server, no geo field types:

SELECT id, ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < 25 ORDER BY distance LIMIT 0 , 20;

This is the haversine formula described here. I'd like to call the query, probably setup as a stored procedure, using LINQ - pulled into an IEnumerable list.

The table essentially looks like this:

Locations Table

  • Id - long PK
  • Name - nvarchar...
  • Lat
  • Lng ...

I'm not familiar with the math functions in MS SQL, the query example is MySQL. Will this query port to MS SQL well? Am I asking for problems attempting to do this?

解决方案

The query you've posted should work reasonably well.

However, if you are in the position where you can change your data schema then SQL Server has some native Geography types - and if you could use these then there are built-in functions and built-in indexing which would allow you to execute this type of query much more efficiently - see http://www.microsoft.com/sqlserver/2008/en/us/spatial-data.aspx

这篇关于SQL查询在LINQ2SQL中基于距lat / lng的距离提取位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆