From 5d9a5772bac19d7f8a394c27e7560becbdb1d6fb Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Fri, 12 Jun 2015 17:23:45 +0200 Subject: [PATCH] Added 'Country' property to Geography --- Logic/Structure/Geography.cs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/Logic/Structure/Geography.cs b/Logic/Structure/Geography.cs index a5badcec..871470d9 100644 --- a/Logic/Structure/Geography.cs +++ b/Logic/Structure/Geography.cs @@ -131,6 +131,33 @@ namespace Swarmops.Logic.Structure return false; } + public Country Country + { + get + { + Countries countries = Countries.All; + Dictionary geoLookup = new Dictionary(); + + foreach (Country country in countries) + { + geoLookup[country.GeographyId] = country; + } + + Geography iterator = this; + while (!geoLookup.ContainsKey (iterator.Identity) && iterator.ParentGeographyId != 0) + { + iterator = iterator.Parent; + } + + if (geoLookup.ContainsKey (iterator.Identity)) + { + return geoLookup[iterator.Identity]; + } + + return null; // no country - we're probably a geography above the country line + } + } + public Geographies ThisAndBelow() { return Geographies.FromArray (GeographyCache.GetGeographyTree (Identity));