public static async Task <decimal> GetDistance(string source, string dest) { using (WorkDBEntities context = new WorkDBEntities()) { //prevent multi records between source and destination manipulation: string[] sortedParams = new string[] { source, dest }.OrderBy(x => x).ToArray(); source = sortedParams[0]; dest = sortedParams[1]; var any = context.GeoDistance.Any(x => x.Source == source && x.Destination == dest); if (!any) { decimal dist = await GetDistAsync(source, dest); if (dist <= 0) { return(dist); } //add it into the DB: var newRecord = new GeoDistance() { Destination = dest, Source = source, KMS = dist }; context.GeoDistance.Add(newRecord); context.SaveChanges(); } var result = context.GeoDistance.First(x => x.Source == source && x.Destination == dest); //update its hits: result.Hits = result.Hits + 1; context.SaveChanges(); return(result.KMS); } }
public GeoDistance[] PopularSearches(int top = 1) { using (WorkDBEntities context = new WorkDBEntities()) { var populars = context.GeoDistance.OrderByDescending(x => x.Hits).Take(top); var res = populars.ToArray(); return(res); } }