Politics, Weather, Photography, and the Dog
Sunday 18 November 2012

I've spent a good bit of free time lately working on migrating Weather Now to Azure. Part of this includes rewriting its Gazetteer, or catalog of places that it uses to find weather stations for users. For this version I'm using Entity Framework 5.0, which in turn allows me to use LINQ extensively.

I always try to avoid duplicating code, and I always try to write sufficient unit tests to prevent (and fix) any coding errors I make. (I also use ReSharper and Visual Studio Code Analysis to keep me honest.)

There are two methods in the Gazetteer's PlaceFinder class that search for places by distance. The prototypes are:

public static IEnumerable FindNearby(ILocatable center, Length radius)


public static IEnumerable FindNearby(ILocatable center, Length radius, Expression<Func<Place, bool>> predicate)

But in order for the first method to work, it has to create a predicate of its own to draw a box around the center location. (The ILocatable interface requires Latitude and Longitude. Length is a class in the Inner Drive Extensible Architecture representing a measurable two-dimensional distance.) So in order for the second method to work, it has to chain predicates.

Fortunately, I found Joe and Ben Albahari's library of LINQ extensions. Here's the second method:

public static IEnumerable<PlaceDistance> FindNearby(
	ILocatable center,
	Length radius,
	Expression<Func<Place, bool>> predicate)
	var searchPredicate = 
		SearchDistancePredicate(center, radius)

	var places = Find(searchPredicate);

	return SearchDistanceResults(places, center, radius);

This allows me to use a single Find method that takes a predicate, engages a retry policy, and returns exactly what I'm looking for. And it allows me to do this, which just blows my mind:

var results = PlaceFinder.FindNearby(TestNode, TestRadius, p => p.Feature.Name == "airport");

Compared with the way Weather Now works under the hood right now, and how much coding the existing code took to achieve the same results, I'm just stunned. And it will make migrating Weather Now a lot easier.

Sunday 18 November 2012 12:34:04 CST (UTC-06:00)  |  | Software | Cool links#
On this page....
Chaining LINQ predicates
The Daily Parker +3422d 08h 04m
Italy 61d 14h 54m
Parker's 9th birthday 79d 05h 09m
My next birthday 160d 09h 14m
Aviation (358) Baseball (110) Best Bars (10) Biking (44) Chicago (966) Cubs (198) Duke (133) Geography (352) Higher Ground (5) Jokes (284) Kitchen Sink (688) London (65) Parker (198) Daily (204) Photography (148) Politics (304) US (1125) World (273) Raleigh (21) Readings (8) Religion (67) San Francisco (93) Software (210) Blogs (82) Business (242) Cloud (90) Cool links (147) Security (103) Travel (255) Weather (743) Astronomy (93) Windows Azure (62) Work (95) Writing (14)
<March 2015>
Full archive
David Braverman and Parker
David Braverman is the Chief Technology Officer of Holden International in Chicago, and the creator of Weather Now. Parker is the most adorable dog on the planet, 80% of the time.
All content Copyright ©2015 David Braverman.
Creative Commons License
The Daily Parker by David Braverman is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 United States License, excluding photographs, which may not be republished unless otherwise noted.
Admin Login
Sign In
Blog Stats
Total Posts: 4735
This Year: 121
This Month: 38
This Week: 10
Comments: 0