Lambda/LINQ Select Minimum

Viewed 8302

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?

3 Answers
Related