我有一个带有Lat / Longs的zipcodes数据库表。我试图找到一些代码,显示一个带有zipcode和x英里的查询,然后返回包含该半径内所有zipcodes的结果集(精度不是很重要 - 只要它很接近)。 / p>
这可以通过Linq to SQL查询来完成,所以我不必使用存储过程吗?
答案 0 :(得分:1)
我想出来了,一旦我找到这个等式,它实际上并不是那么难。
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