asp.net - 使用LINQ从数据库中查找附近的地方

标签 asp.net .net linq c#-4.0 sql-to-linq-conversion

我们希望在 ASP.NET 2012 中使用 LINQ 从数据库接收附近地点的列表,并希望对我们的策略提供一些反馈。

我的表和假数据:

     PlaceId    Name       Latitude   Longitude 
       1          A          18.1        20.1
       2          B          18.2        20.2
       3          C          18.3        20.3

1)在我们的项目中,客户端当前位置(纬度和经度)作为输入

2) 在服务器端,根据客户端当前位置,我们需要使用 LINQ 从数据库中找到附近的地方

这是我之前使用的 SQL 代码,但现在我们想使用 LINQ。
SELECT  name, Latitude, Longitude , 
  ( 3959 * acos( cos( radians(?) )* cos( radians( Latitude) ) * cos( radians( Longitude ) - radians(?) ) 
 + sin( radians(?) ) * sin( radians( Latitude) ) ) ) AS distance 
FROM TABLE_NAME 
HAVING distance < ? 
ORDER BY distance LIMIT 0 , 20

[但问题是如何在 LINQ 中编写这样的查询。]

我在这方面的工作:

在寻找解决方案时,我遇到了这段代码
        var Value1 = 57.2957795130823D;
        var Value2 = 3958.75586574D;

        var searchWithin = 20;

    double latitude = ConversionHelper.SafeConvertToDoubleCultureInd(Latitude, 0),
          longitude = ConversionHelper.SafeConvertToDoubleCultureInd(Longitude, 0);

    var location = (from l in sdbml.Places
                    let temp = Math.Sin(Convert.ToDouble(l.Latitude) / Value1) *  Math.Sin(Convert.ToDouble(latitude) / Value1) +
                             Math.Cos(Convert.ToDouble(l.Latitude) / Value1) *
                             Math.Cos(Convert.ToDouble(latitude) / Value1) *
                             Math.Cos((Convert.ToDouble(longitude) / Value1) - (Convert.ToDouble(l.Longitude) / Value1))
                         let calMiles = (Value2 * Math.Acos(temp > 1 ? 1 : (temp < -1 ? -1 : temp)))
                         where (l.Latitude > 0 && l.Longitude > 0)
                         orderby calMiles
                        select new location
                             {
                                    Name = l.name
                                });
                        return location .ToList();

但问题是,如何引用 ConversionHelper 或它来自哪个命名空间。

感谢所有建议。

最佳答案

因此,如果您只想计算两个坐标之间的距离,为什么不使用 Dot Net 的 GeoCoordinate ?

它像

 var firstCordinate = new GeoCoordinate(latitude1, longitude1);
 var secondCordinate = new GeoCoordinate(latitude2, longitude2);

 double distance = firstCordinate.GetDistanceTo(secondCordinate);

你可以在命名空间中找到它 System.Device.Location .

因此,这将使您免于所有这些 Math.CosMath.Sin并且您的 linq 将简单明了。 (可能 foreach 循环会做)

所以你的整个查询可以总结为:
List<Location> locations = new List<Location>();
foreach(var place in sdbml.Places)
{
   //your logic to compare various place's co-ordinates with that of
   //user's current co-ordinate
}

关于asp.net - 使用LINQ从数据库中查找附近的地方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16356302/

相关文章:

c# - 来自 Azure Blob 下载图像的链接,但我只需要观看(Asp.NET Core)

c# - ASP.NET-Telerik : Change size Marker

asp.net - 如何确定 Facebook 是否访问了我的页面?

c# - 正则表达式前瞻在 .NET 中不起作用

.net - 如何在不使用 Tab 或鼠标的情况下将光标从第一个文本区域移动到第二个文本区域?

c# - Entity Framework 4.1 Linq Contains 和 StartsWith

asp.net - 覆盖asp.net/IIS错误页面

c# - ASP.Net MVC 3 - 在 POST 中更改 URL

c# - 使用 OrderBy、ThenBy 排序

c# - 在 SelectMany LINQ 中使用索引