LINQ 条件串联查询
我是一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我喜欢 Linq 和字典:
I love Linq and dictionaries: