Linq: calling the same method for Where and OrderBy only once instead of twice?

Viewed 844

I have a simple linq query where I need to filter stores within a certain distance and also order by the distance calculation result, you get the idea.

So, I ended up calling GetDistance method twice for now. How can I optimize code to call it only once per store?

double distance = 50;

var result = stores.Where<MyStore>( s =>
                 Helper.GetDistance( lat, lon, s.Lat, s.Lon ) <= distance )
                .OrderBy( s => Helper.GetDistance( lat, lon, s.Lat, s.Lon ) )
                .ToList();
2 Answers
Related