Exemplo n.º 1
0
        public DistributionCenter GetNearest(Address address) {
            EcommercePlatformDataContext db = new EcommercePlatformDataContext();
            DistributionCenter distcenter = new DistributionCenter();
            try {
                double earth = 3963.1676; // radius of Earth in miles
                DCList d = (from dc in db.DistributionCenters
                            where dc.State1.countryID == address.State1.countryID
                            select new DCList {
                                ID = dc.ID,
                                distance = earth * (
                                                    2 * Math.Atan2(
                                                        Math.Sqrt((Math.Sin((Convert.ToDouble(dc.Latitude - address.latitude) * (Math.PI / 180)) / 2) * Math.Sin((Convert.ToDouble(dc.Latitude - address.latitude) * (Math.PI / 180)) / 2)) + ((Math.Sin((Convert.ToDouble(dc.Longitude - address.longitude) * (Math.PI / 180)) / 2)) * (Math.Sin((Convert.ToDouble(dc.Longitude - address.longitude) * (Math.PI / 180)) / 2))) * Math.Cos(Convert.ToDouble(address.latitude) * (Math.PI / 180)) * Math.Cos(Convert.ToDouble(dc.Latitude) * (Math.PI / 180))),
                                                        Math.Sqrt(1 - ((Math.Sin((Convert.ToDouble(dc.Latitude - address.latitude) * (Math.PI / 180)) / 2) * Math.Sin((Convert.ToDouble(dc.Latitude - address.latitude) * (Math.PI / 180)) / 2)) + ((Math.Sin((Convert.ToDouble(dc.Longitude - address.longitude) * (Math.PI / 180)) / 2)) * (Math.Sin((Convert.ToDouble(dc.Longitude - address.longitude) * (Math.PI / 180)) / 2))) * Math.Cos(Convert.ToDouble(address.latitude) * (Math.PI / 180)) * Math.Cos(Convert.ToDouble(dc.Latitude) * (Math.PI / 180))))
                                                    )
                                                )
                            }).OrderBy(x => x.distance).First<DCList>();

                distcenter = db.DistributionCenters.Where(x => x.ID == d.ID).First<DistributionCenter>();
            } catch {
                DCList d = (from dc in db.DistributionCenters
                            where dc.State1.countryID == address.State1.countryID
                            select new DCList {
                                ID = dc.ID,
                                distance = 0
                            }).OrderBy(x => x.distance).First<DCList>();
                distcenter = db.DistributionCenters.Where(x => x.ID == d.ID).First<DistributionCenter>();
            };
            return distcenter;
        }
		private void detach_DistributionCenters(DistributionCenter entity)
		{
			this.SendPropertyChanging();
			entity.State1 = null;
		}
 partial void UpdateDistributionCenter(DistributionCenter instance);
 partial void DeleteDistributionCenter(DistributionCenter instance);
 partial void InsertDistributionCenter(DistributionCenter instance);