使用LINQ找到数据库附近的地方(Using LINQ find nearby places fro

2019-09-01 21:01发布

我们要收到2012 ASP.NET使用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或根据该命名空间而来。

所有的建议表示赞赏。

Answer 1:

所以,如果所有你想要在两个坐标之间计算距离,你为什么不使用点网的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
}


Answer 2:

这里是我终于有安定下来的代码

1)创建一个类,说

DistanceModel.cs

public class DistanceModel
{
   public int PlaceId { get; set; }

   public string Name { get; set; }

   public double Latitute { get; set; }

   public double Longitude { get; set; }

} 

2)然后包括哪个文件,你想下面的代码,说

MainPage.cs

     /*Call GetAllNearestFamousPlaces() method to get list of nearby places depending 
      upon user current location.
      Note: GetAllNearestFamousPlaces() method takes 2 parameters as input
     that is GetAllNearestFamousPlaces(user_current_Latitude,user_current_Longitude) */


   public void GetAllNearestFamousPlaces(double currentLatitude,double currentLongitude)
    {
        List<DistanceModel> Caldistance = new List<DistanceModel>();
        var query = (from c in sdbml.Places
                     select c).ToList();
        foreach (var place in query)
        {
            double distance = Distance(currentLatitude, currentLongitude, place.Latitude, place.Logitude);
            if (distance < 25)          //nearbyplaces which are within 25 kms 
            {
                DistanceModel dist = new DistanceModel();
                dist.Name = place.PlaceName;
                dist.Latitute = place.Latitude;
                dist.Longitude = place.Logitude;
                dist.PlaceId = place.PlaceId;
                Caldistance.Add(getDiff);
            }
        }                      
    }

   private double Distance(double lat1, double lon1, double lat2, double lon2)
    {
        double theta = lon1 - lon2;
        double dist = Math.Sin(deg2rad(lat1)) * Math.Sin(deg2rad(lat2)) + Math.Cos(deg2rad(lat1)) * Math.Cos(deg2rad(lat2)) * Math.Cos(deg2rad(theta));
        dist = Math.Acos(dist);
        dist = rad2deg(dist);
        dist = (dist * 60 * 1.1515) / 0.6213711922;          //miles to kms
        return (dist);
    }

   private double deg2rad(double deg)
    {
        return (deg * Math.PI / 180.0);
    }

   private double rad2deg(double rad)
    {
        return (rad * 180.0 / Math.PI);
    }

它的工作对我来说,希望它会帮助你。



文章来源: Using LINQ find nearby places from database