[英]C# - LINQ - shortest distance by GPS latitude and longitude
I have database and in it I have class hotel with gps coordinates. 我有数据库,在其中我有gps坐标的类酒店。 I want to get closest places to coordinates which I choose.
我想获得最接近我选择的坐标的位置。
I think It should look like this (I found many example codes here and like this one): 我认为它看起来应该是这样的(我在这里发现了许多示例代码并且像这样):
var coord = new GeoCoordinate(latitude, longitude);
var nearest = (from h in db.hotels
let geo = new GeoCoordinate(h.gps.lat, h.gps.lng)
orderby geo.GetDistanceTo(coord)
select h).Take(10);
The problem is that I have this error when I tried to search for something: 问题是当我尝试搜索某些内容时出现此错误:
Only parameterless constructors and initializers are supported in LINQ to Entities
LINQ to Entities中仅支持无参数构造函数和初始值设定项
I tried to google it and I found that dividing that linq into two can help me but I am not sure how. 我试图谷歌它,我发现将linq分成两个可以帮助我,但我不知道如何。 Thanks for help.
感谢帮助。
You can use the object initializer instead of parameterized constructor: 您可以使用对象初始值设定项而不是参数化构造函数:
var nearest = (from h in db.hotels
let geo = new GeoCoordinate{ Latitude = h.gps.lat, Longitude = h.gps.lng}
orderby geo.GetDistanceTo(coord)
select h).Take(10);
But you will likely have problems caused by the GetDistanceTo method, could you provide the implementation of that method? 但是你可能会遇到GetDistanceTo方法引起的问题,你能提供该方法的实现吗?
I have had a reasonable about of success using this implementation of the Haversine Distance formula 使用Haversine Distance公式的这种实现,我有一个合理的成功
var startPoint = new { Latitude = 1.123, Longitude = 12.3 };
var closest = entities.Something.OrderBy(x => 12742 * SqlFunctions.Asin(SqlFunctions.SquareRoot(SqlFunctions.Sin(((SqlFunctions.Pi() / 180) * (x.Latitude - startPoint.Latitude)) / 2) * SqlFunctions.Sin(((SqlFunctions.Pi() / 180) * (x.Latitude - startPoint.Latitude)) / 2) +
SqlFunctions.Cos((SqlFunctions.Pi() / 180) * startPoint.Latitude) * SqlFunctions.Cos((SqlFunctions.Pi() / 180) * (x.Latitude)) *
SqlFunctions.Sin(((SqlFunctions.Pi() / 180) * (x.Longitude - startPoint.Longitude)) / 2) * SqlFunctions.Sin(((SqlFunctions.Pi() / 180) * (x.Longitude - startPoint.Longitude)) / 2)))).Take(5);
I post here my solution which I am using for now. 我在这里发布我现在使用的解决方案。 But I choose GwynnBliedd answer because he solves my problem in question.
但我选择了GwynnBliedd的答案,因为他解决了我的问题。
I added DbGeography type to my class and I am using it instead of saving latitude and longitude in database. 我将DbGeography类型添加到我的班级,我使用它而不是在数据库中保存纬度和经度。
locationField = DbGeography.FromText(String.Format("POINT({0} {1})", orig.gps.lat.ToString().Replace(",", "."), orig.gps.lng.ToString().Replace(",", ".")));
Then it's very easy to use linq: 然后使用linq很容易:
var coord = DbGeography.FromText(String.Format("POINT({0} {1})", latitude.ToString().Replace(",", "."), longitude.ToString().Replace(",", ".")));
var nearest = (from h in db.hotels
where h.location != null
orderby h.location.Distance(coord)
select h).Take(limit);
return nearest;
For now this is working solution and it's good. 目前这是一个有效的解决方案,它很好。 For sometime I would be using this but as few users said here I'll maybe try UDF with implementing Haversine formula (like in this answer ).
有一段时间我会使用这个,但很少有用户在这里说我可能会尝试使用UDF来实现Haversine公式(就像在这个答案中一样 )。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.