Walter.BOM
Expand Method
Example 
Walter.BOM Assembly > Walter.BOM.Geo Namespace > GeoExtensionMethods Class : Expand Method
An array of locations that may include both countries and regions.
Expands the specified array of locations, converting any regions within the array into their constituent countries, and returns a comprehensive array of individual GeoLocation values.
Syntax
public static GeoLocation[] Expand( 
   GeoLocation[] locations
)

Parameters

locations
An array of locations that may include both countries and regions.

Return Value

An array of GeoLocation values representing the most detailed geographic entities. If a location is a region, it is expanded into its constituent countries; individual countries are returned as-is.
Remarks
This method is particularly useful for operations requiring detailed geographic granularity, such as data analysis, reporting, or user segmentation by country. It ensures that all geographic entities are represented at the country level, facilitating uniformity and precision in geographic-related processing.
Example

Let's consider a use case where a developer working on an e-commerce platform needs to apply specific shipping rules, tax calculations, or promotional offers based on the customer's geographic location. In this scenario, the platform supports both country-specific and region-wide promotions or rules. However, the challenge arises when regions overlap, potentially leading to the same country being considered more than once for promotions, which can cause discrepancies in the application of rules or offers.

Use Case: Applying Geographic-Specific Promotions Without Duplication

Background: An e-commerce platform offers special promotions for customers in Latin America and South America. Some promotions are specific to countries, while others apply to entire regions. Given the overlap between these regions (e.g., countries like Brazil are part of both Latin America and South America), there's a risk of applying the same promotion multiple times to customers from overlapping countries.

Challenge: Ensure that each eligible country within overlapping regions is identified uniquely to apply promotions accurately without duplication, even when regions overlap.

Solution: Use the Expand method to convert regional representations into unique, constituent country-level GeoLocations. This approach guarantees that each country is considered only once when determining eligibility for promotions, regardless of regional overlaps.

using Walter.BOM.Geo;
            
             Console.WriteLine("Enter 2 letter country code of a latin america country");
             GeoLocationMapping.TryGetValue(Console.ReadLine()?.Trim(),out var choice);
            
             // Sample regions with potential overlap
             var promotionalRegions = new[] { GeoLocation.LATIN_AMERICA, GeoLocation.SOUTH_AMERICA };
            
             // Expand regions into a unique list of constituent countries
             var eligibleCountries = promotionalRegions.Expand().Distinct();
            
             // Example function to apply promotions based on country
             void ApplyPromotions(IEnumerable<GeoLocation> countries)
             {
                 var color= Console.ForegroundColor;
                 foreach (var country in countries)
                 {
                     // Logic to apply country-specific promotions
            
                     if (country == choice)
                     { 
                         Console.ForegroundColor = ConsoleColor.Green;
                         Console.WriteLine("Discount is applicable for {0}", choice);
                     }
                     else
                     { 
                         Console.ForegroundColor = ConsoleColor.Red;
                         Console.WriteLine($"Skipping promotions for {country}.");
                     }
                 }
                 Console.ForegroundColor = color;
             }
            
             // Apply promotions without duplication
             ApplyPromotions(eligibleCountries);
            
             #if !DEBUG
             Console.WriteLine("press any key to exit");
             Console.ReadKey();
             #endif
Requirements

Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also

Reference

GeoExtensionMethods Class
GeoExtensionMethods Members