使用 Linq to Sql 查找半径距离内的邮政编码

发布于 2024-08-09 04:53:21 字数 150 浏览 8 评论 0原文

我有一个邮政编码及其纬度/经度的数据库表。我试图找到一些代码,显示一个查询,该查询采用邮政编码和 x 英里,然后返回包含该半径内所有邮政编码的结果集(精度不是很重要 - 只要接近即可)。

是否可以通过 Linq to SQL 查询来完成此操作,这样我就不必使用存储过程?

I have a database table of zipcodes with their Lat/Longs. I'm trying to find some code that shows a query that takes a zipcode and x miles and then return set of results that include all the zipcodes that are within that radius (precision is not very important - as long as it's close).

Can this be done with a Linq to SQL query so I don't have to use a Stored Procedure?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明媚如初 2024-08-16 04:53:23

我想通了,一旦我找到了方程,实际上并没有那么难。

Public Function SearchStudents(ByVal SearchZip As String, ByVal Miles As Double) As IEnumerable(Of Student)
                Dim dc As New IMDataContext()

                Dim lat As Double
                Dim lng As Double
                Dim maxlat As Double
                Dim minlat As Double
                Dim maxlng As Double
                Dim minlng As Double

                Dim zip As ZipCode = (From z In dc.ZipCodes Where z.ZipCode = SearchZip).SingleOrDefault()

                lat = zip.Latitude
                lng = zip.Longitude

                maxlat = lat + Miles / 69.17
                minlat = lat - (maxlat - lat)
                maxlng = lng + Miles / (Math.Cos(minlat * Math.PI / 180) * 69.17)
                minlng = lng - (maxlng - lng)

                Dim ziplist = From z In dc.ZipCodes Where z.Latitude >= minlat _
                       And z.Latitude <= maxlat _
                       And z.Longitude >= minlng _
                       And z.Longitude <= maxlng Select z.ZipCode

                Return From i In dc.Students Where ziplist.Contains(i.Zip)
            End Function

I figured it out and it actually wasn't all that hard once i found the equation.

Public Function SearchStudents(ByVal SearchZip As String, ByVal Miles As Double) As IEnumerable(Of Student)
                Dim dc As New IMDataContext()

                Dim lat As Double
                Dim lng As Double
                Dim maxlat As Double
                Dim minlat As Double
                Dim maxlng As Double
                Dim minlng As Double

                Dim zip As ZipCode = (From z In dc.ZipCodes Where z.ZipCode = SearchZip).SingleOrDefault()

                lat = zip.Latitude
                lng = zip.Longitude

                maxlat = lat + Miles / 69.17
                minlat = lat - (maxlat - lat)
                maxlng = lng + Miles / (Math.Cos(minlat * Math.PI / 180) * 69.17)
                minlng = lng - (maxlng - lng)

                Dim ziplist = From z In dc.ZipCodes Where z.Latitude >= minlat _
                       And z.Latitude <= maxlat _
                       And z.Longitude >= minlng _
                       And z.Longitude <= maxlng Select z.ZipCode

                Return From i In dc.Students Where ziplist.Contains(i.Zip)
            End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文