Skip to content
This repository has been archived by the owner on Nov 18, 2024. It is now read-only.

Commit

Permalink
Do distance computations outside the life of the SqlConnection.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonsagara committed Feb 28, 2020
1 parent 0e815cb commit 3accd14
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/LocationByZip/SqlLocationRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public async Task<IReadOnlyCollection<Location>> GetByCityStateAsync(string city
/// within Radius miles of inRefLoc.</returns>
public async Task<IReadOnlyCollection<LocationInRadius>> GetLocationsInRadiusAsync(Location origin, RadiusBox bounds)
{
var locationsInBoundingBox = new List<LocationInRadius>();
var locationsInRadius = new List<LocationInRadius>();

using (var conn = new SqlConnection(_connectionString))
Expand All @@ -72,17 +73,16 @@ public async Task<IReadOnlyCollection<LocationInRadius>> GetLocationsInRadiusAsy
EastLon = bounds.RightLongitude,
};

var locs = (await conn.QueryAsync<LocationInRadius>(GetLocationsWithinRadiusSql(), args))
.ToArray();
locationsInBoundingBox.AddRange(await conn.QueryAsync<LocationInRadius>(GetLocationsWithinRadiusSql(), args));
}

foreach (var loc in locs)
{
loc.DistanceToCenter = loc.DistanceFrom(origin);
foreach (var locInBox in locationsInBoundingBox)
{
locInBox.DistanceToCenter = locInBox.DistanceFrom(origin);

if (loc.DistanceToCenter <= bounds.RadiusMiles)
{
locationsInRadius.Add(loc);
}
if (locInBox.DistanceToCenter <= bounds.RadiusMiles)
{
locationsInRadius.Add(locInBox);
}
}

Expand Down

0 comments on commit 3accd14

Please sign in to comment.