C# - LINQ - 通过GPS纬度和经度的最短距离 [英] C# - LINQ - shortest distance by GPS latitude and longitude

查看:494
本文介绍了C# - LINQ - 通过GPS纬度和经度的最短距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有数据库,在这里我有带GPS坐标的班级酒店。我想要找到最接近我所选择的坐标的地方。



我认为应该是这样(我在这里找到很多示例代码,像这样):

  var coord = new GeoCoordinate(纬度,经度); 
var nearest =(from h in db.hotels
let geo = new GeoCoordinate(h.gps.lat,h.gps.lng)
orderby geo.GetDistanceTo(coord)
选择h)。

问题是我尝试搜索某些东西时出现此错误:


只有无参数的构造函数和初始值设置在LINQ
中支持实体


我试图google,我发现将linq分成两个可以帮助我,但我不知道如何。感谢您的帮助。

解决方案

您可以使用对象初始化器而不是参数化构造函数:

  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);

但是您可能会遇到由GetDistanceTo方法引起的问题,您可以提供该方法的实现吗?


I have database and in it I have class hotel with gps coordinates. 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

I tried to google it and I found that dividing that linq into two can help me but I am not sure how. 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?

这篇关于C# - LINQ - 通过GPS纬度和经度的最短距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆