2

I'm a LINQ newbie working on a GIS project involving international postal addresses. One of my design problems involves dynamically transforming decomposed address data in key/value pair format into a multi-line postal address format by country. Each record will need to self-compose based on the Country field value, according a set of rules defined by country and address line:

Dictionary<string, string> addressOne = new Dictionary<string,string>() {
    { "StreetName", "Stradă Măguricea" },
    { "HouseNumber", "1" },
    { "ApartmentLabel", "Ap" },
    { "ApartmentNumber", "17" },
    { "PostalCode", "014231" },
    { "City", "BUCUREŞTI" },
    { "Country", "Romania" }
};

Dictionary<string, string> addressTwo = new Dictionary<string,string>() {
    { "StreetName", "PORTAGE" },
    { "StreetSuffix", "AVE" },
    { "HouseNumber", "811" },
    { "City", "WINNIPEG" },
    { "StateProvince", "MB" },
    { "PostalCode", "R3B 2A8" },
    { "Country", "CANADA" }
};

//Example Concatenation Rules (these are approximations)...

//Romania:  AddressLine1 = "{StreetName}[ {StreetSuffix}] {HouseNumber}[, {ApartmentLabel} {ApartmentNumber}"
//          AddressLine2 = "{PostalCode} {City}"
//          AddressLine3 = "{Country}"

//Canada:   AddressLine1 = "{HouseNumber} {StreetName}[ {StreetSuffix}]"
//          AddressLine2 = "[{ApartmentLabel} {ApartmentNumber}]"
//          AddressLine3 = "{City} {StateProvince} {PostalCode}"
//          AddressLine4 = "{Country}"

I'm currently planning a function table that calls by Country and AddressLine, with each Func returning a composite field formatted by the appropriate concatenation rule. I can always implement these rule functions with traditional StringBuildler logic, but this seems ripe for a LINQ query.

Most of my searching comes up with the usual comma aggregation scenarios; this is more of a selective, conditional pattern matching problem (I can smell the Regex, already). Is this a good use-case for LINQ, or should I stick to old-school branching string operations?

Thanks for reading!

Community
  • 1
  • 1
KurtisR
  • 45
  • 5
  • Is each country going to have its own dictionary of address fields and values? Also, how will the format (i.e., address lines) for each country be defined in terms of an object? Will it be a list of strings with each index being the position of the address line? Finally, are square brackets optional fields? Alternately if it doesn't match any key in the dictionary it simply doesn't exist. Answering these questions should help clarify some issues. – Ahmad Mageed Mar 30 '11 at 15:32

1 Answers1

1

I love Linq and dictionaries:

public static IEnumerable<string> InjectFields(IDictionary<string, string> address)
{
    string country = address["Country"];
    return formatStrings[country]
            .Select(s => address.Aggregate(s, (acc, pair) => acc.Replace("{"+pair.Key+"}", pair.Value)))
            .ToArray();
}

private static Dictionary<string, IEnumerable<string>> formatStrings = 
             new Dictionary<string, IEnumerable<string>>(StringComparer.InvariantCultureIgnoreCase)
                    {
                        { "Romania", new[] {
                            "{StreetName}[ {StreetSuffix}] {HouseNumber}[, {ApartmentLabel} {ApartmentNumber}",
                            "{PostalCode} {City}",
                            "{Country}",
                        } },
                        { "Canada", new[] {
                            "{HouseNumber} {StreetName}[ {StreetSuffix}]",
                            "[{ApartmentLabel} {ApartmentNumber}]",
                            "{City} {StateProvince} {PostalCode}",
                            "{Country}"
                        } },
                    };
Snowbear
  • 16,924
  • 3
  • 43
  • 67