LINQ 条件串联查询

发布于 2024-10-28 19:03:44 字数 1691 浏览 0 评论 0原文

我是一个 LINQ 新手,正在从事涉及国际邮政地址的 GIS 项目。我的设计问题之一涉及将键/值对格式的分解地址数据动态转换为按国家/地区划分的多行邮政地址格式。每条记录都需要根据国家/地区字段值、根据国家/地区和地址行定义的一组规则自行编写:

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}"

我目前正在计划一个由 国家/地区< 调用的函数表/code> 和 AddressLine,每个 Func 返回一个由适当的串联规则格式化的复合字段。我始终可以使用传统的 StringBuildler 逻辑来实现这些规则函数,但这对于 LINQ 查询来说似乎已经成熟。

我的大部分搜索都会出现常见的逗号聚合场景;这更多的是一个选择性的、条件模式匹配问题(我已经闻到了正则表达式的味道)。这是 LINQ 的一个很好的用例,还是我应该坚持老式的分支字符串操作?

感谢您的阅读!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魂牵梦绕锁你心扉 2024-11-04 19:03:44

我喜欢 Linq 和字典:

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

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}"
                        } },
                    };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文