I'm writing a function using the .NET GeoCoordinate
class. We have an Airport
class and a City
class, both of which define their own GeoCoordinate
.
I need to select the nearest airport relative to the city, and I am trying to do so using the GetDistanceTo()
method.
What I have right now looks something like this:
Airport a = Airports.GetAllActiveAirports().Min(this.Coordinates.GetDistanceTo(n.Profile.Coordinates));
Another (working) function that retrieves a list of nearest airports by distance uses:
List<Airports> airports = Airports.GetAllActiveAirports();
var nearby =
from a in airports
where this.Coordinates.GetDistanceTo(a.Profile.Coordinates) > d
select a;
foreach(Airport a in nearby)
{
airports.Remove(a);
}
I've seen examples of doing things like this in a single line with LINQ & lambdas, but I'm not entirely sure how to execute this one...any pointers?
If I get your question, this line gets the minimum distance from Coordinates to an active airport.
Airports.GetAllActiveAirports().Min(_ => Coordinates.GetDistanceTo(_.Profile.Coordinates))
If you want the airport in question then:
var airports = Airports.GetAllActiveAirports();
var closest = airports.First(_ => Coordinates.GetDistanceTo(_.Profile.Coordinates) == airports.Min(_ => Coordinates.GetDistanceTo(_.Profile.Coordinates)))
You don't have to keep it in one line... Visual Studio won't run out of space.
An even better option, without getting the minimum in every iteration would be:
var airports = Airports.GetAllActiveAirports();
var minDistance = airports.Min(_ => Coordinates.GetDistanceTo(_.Profile.Coordinates))
var closest = airports.First(_ => Coordinates.GetDistanceTo(_.Profile.Coordinates) == minDistance)
The accepted answer causes 2 calls to GetDistance
for each airport. Here's how you can do it in a single pass:
var closestAirport = Airports.GetAllActiveAirports()
.Select(x => new {
Airport = x,
Distance = this.Coordinates.GetDistanceTo(x.Profile.Coordinates)})
.Aggregate((a1, a2) => a1.Distance < a2.Distance ? a1 : a2)
.Airport;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With