linq GeoCoordinate基于两个表选择

时间:2017-02-28 23:25:36

标签: c# sql linq

第一个模型代表地理数据

public class GeoData
{
    public int ID { get; set; }
    public string CountryCode { get; set; }
    public string PostalCode { get; set; }
    public string PlaceName { get; set; }
    public double Latitude { get; set; }
    public double Longitude { get; set; }        
}

第二个模型代表用户的地址

public class Address
{
    public int ID { get; set; }
    [Required]
    public int GeoDataID { get; set; }
    [Required]
    public string UserID { get; set; }
}

GeoDataID是GeoData.ID的FK

给定方便的用户ID和范围,我想检索该范围内的所有地址。 我的第一个方法

  1. 接收用户ID和范围作为输入
  2. 调用另一个方法,返回链接到用户地址的GeoCoordinate
  3. 调用一个静态方法,要求List将用户的GeoCoordinate,range和db context作为params传递

    public async Task<ActionResult> Index(string userid, int range)
    {
            addressViewModel.Addresses = GeoDataController.getCoordinates(getGeoData(userid), db);
    
  4. 静态方法应该负责检索范围内的GeoCoordinate,并返回GeoDataID等于GeoData PK的地址列表。

    public static List<Address> getCoordinates(GeoCoordinate centerArea, GeoContext gdb, int range)
        {
            List<Address> PlacesInRange = new List<Address>();
                var query = from p in gdb.GeoData
                            select p;
                foreach (var geoArea in query)
                {
                    GeoCoordinate geoCoordinate = new GeoCoordinate
                    {
                        Latitude = geoArea.Latitude,
                        Longitude = geoArea.Longitude
                    };
    
                    //adding to list only those Geocoordinates in a given range from centerArea
                    if (centerArea.GetDistanceTo(geoCoordinate) < range)
                    {
                    //retrieve Address corresponding to current GeoData record
                    Address AddressInRange = gdb.Addresses
                            .Where(a => a.GeoDataID == GeoData.ID).SingleOrDefault();
                    // add found address to List
                        PlacesInRange.Add(AddressInRange);
                    }
                }
    
            return PlacesInRange;
        }
    

    这不仅非常糟糕,因为我正在检索所有GeoData记录,然后过滤......但是当它涉及地址时却不起作用......

    There is already an open DataReader associated with this Command which must be closed first
    

    我不会花任何时间来解决open datareader问题,因为如果GeoCoordinate(双纬度,双经度)在给定用户的地址范围。 然而,我无法解决这个问题,这就是为什么我们会感激任何帮助。

0 个答案:

没有答案