Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 使用LINQ从数据库中查找附近的位置_Asp.net_.net_Linq_C# 4.0_Sql To Linq Conversion - Fatal编程技术网

Asp.net 使用LINQ从数据库中查找附近的位置

Asp.net 使用LINQ从数据库中查找附近的位置,asp.net,.net,linq,c#-4.0,sql-to-linq-conversion,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

我们希望在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
选择名称、纬度、经度、,
(3959*acos(弧度(?)*cos(弧度(纬度))*cos(弧度(经度)-弧度(?)
+sin(弧度(?)*sin(弧度(纬度))作为距离
从表\u名称
距离小于?
按距离限制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();
var值1=57.2957795130823D;
var值2=3958.75586574D;
var=20;
double latitude=ConversionHelper.SafeConvertToDoubleCultureInd(纬度,0),
经度=ConversionHelper.SafeConvertToDoubleCultureInd(经度,0);
变量位置=(从sdbml.Places中的l开始)
设temp=Math.Sin(Convert.ToDouble(l.Latitude)/Value1)*Math.Sin(Convert.ToDouble(Latitude)/Value1)+
Math.Cos(转换为双(l.Latitude)/Value1)*
Math.Cos(转换为双精度(纬度)/Value1)*
Math.Cos((Convert.ToDouble(经度)/Value1)-(Convert.ToDouble(经度)/Value1))
设calMiles=(Value2*Math.Acos(temp>1?1:(temp<-1?-1:temp)))
其中(l.纬度>0和l.经度>0)
卡米尔斯医嘱
选择新位置
{
Name=l.Name
});
返回location.ToList();
但问题是,如何引用ConversionHelper或它位于哪个名称空间下


非常感谢您的建议。

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

事情是这样的

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

 double distance = firstCordinate.GetDistanceTo(secondCordinate);
您可以在名称空间
System.Device.Location
中找到它

因此,这将使您从所有的
Math.Cos
Math.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
}
列表位置=新列表();
foreach(sdbml.Places中的变量位置)
{
//你的逻辑是比较各个地方的坐标和
//用户当前坐标
}

这是我最后不得不接受的准则

1) 创建一个类,比如

距离模型

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);
    }
/*调用GetAllNearestFamousPlaces()方法获取附近位置的列表,具体取决于
根据用户当前位置。
注意:GetAllNearestFamousPlaces()方法接受2个参数作为输入
即GetAllNearestFamousPlaces(用户当前纬度,用户当前经度)*/
public void GetAllNearestFamousPlaces(双当前纬度,双当前经度)
{
List Caldistance=新列表();
var query=(来自sdbml.Places中的c)
选择c.ToList();
foreach(查询中的变量位置)
{
双距离=距离(当前纬度,当前经度,地点。纬度,地点。逻辑度);
if(距离<25)//距离在25公里以内的附近地方
{
DistanceModel dist=新的DistanceModel();
dist.Name=place.PlaceName;
纬度区=地点纬度;
距离经度=地点经度;
dist.PlaceId=place.PlaceId;
Caldistance.Add(getDiff);
}
}                      
}
专用双倍距离(双倍lat1、双倍lon1、双倍lat2、双倍lon2)
{
双θ=lon1-lon2;
双区=数学Sin(deg2rad(lat1))*数学Sin(deg2rad(lat2))+数学Cos(deg2rad(lat1))*数学Cos(deg2rad(lat2))*数学Cos(deg2rad(theta));
dist=数学Acos(dist);
dist=rad2deg(dist);
dist=(dist*60*1.1515)/0.6213711922;//英里到公里
返回(dist);
}
专用双deg2rad(双deg)
{
返回值(deg*Math.PI/180.0);
}
专用双rad2deg(双rad)
{
返回值(rad*180.0/Math.PI);
}

它对我有用,希望能帮助您。

为什么不使用存储过程?您可以将存储过程的结果映射到对象,映射方式与映射表的方式大致相同,例如getClosePlaces_result。对不起,我应该提到,如果您想要的话,您可以指定返回类型为位置列表。@JuannStrauss除了明显的代码依赖性后果,OP请求如何在C#-)中执行此操作这就是为什么我添加了一条评论而不是回答。