开发者

LINQ conditional concatenation queries

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!


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}"
                        } },
                    };
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜