public IList<office> GetNearestOffices(OfficeQuery qry)
        {
            using (var db = new ExcerciseEntities())
            {
                // Find coordinates in a rectangle instead of a radius to improve performance. 
                // The index on the latitude and longitude columns will be hit in the database.
                var boundingBox = Geography.GetBoundingBox(new MapPoint() { Latitude=qry.Latitude,Longitude=qry.Longitude}, qry.Radius);

                var query = db.offices.Where(v =>
                                            (v.latitude >= boundingBox.MinPoint.Latitude && v.latitude <= boundingBox.MaxPoint.Latitude
                                             && v.longitude >= boundingBox.MinPoint.Longitude && v.longitude <= boundingBox.MaxPoint.Longitude
                                            ) 
                                            && (!qry.IsOpenInWeekends || v.is_open_in_weekends=="Y") 
                                            && (!qry.IsWithSupportDesk || v.has_support_desk=="Y")
                                        );

                return query.Distinct().ToList<office>();
            }
        }
Esempio n. 2
0
        public IList<OfficeBusinessDistance> GetNearestOffices(OfficeQuery qry)
        {
            if (OfficeRepository == null) throw new ArgumentNullException("Officerepository not defined");

            return OfficeRepository.GetNearestOffices(qry).Select(t => new OfficeBusinessDistance()
            {
                City = t.city,
                HasSupportDesk = (t.has_support_desk == "Y"),
                IsOpenInWeekends = (t.is_open_in_weekends=="Y"),
                Street = t.street,
                Latitude = t.latitude,
                Longitude = t.longitude,
                Id = t.id,
                Distance = Geography.HaversineDistance(new MapPoint() { Latitude = t.latitude, Longitude = t.longitude },
                                                       new MapPoint() { Latitude=qry.Latitude,Longitude=qry.Longitude},
                                                       Geography.DistanceUnit.Kilometers)

            })
            //Correct rounding errors
            .Where(t=>t.Distance<=qry.Radius)
            //Sort by distance
            .OrderBy(t=>t.Distance).ToList();
        }