从字典序列中提取主键数据值
我编写了以下代码来提取字典序列中 ID 字段的值 并将它们作为一个集合返回 - 基本上我正在寻找数据库表中行的 PK 值 我曾经填充过字典(每个表行 1 个字典)。代码下面是一些示例数据 被加载到字典序列中。
虽然下面的代码有效,但样式可能更实用 - 我不得不求助于 使用可变列表来构建结果集。所以我觉得不合适。
任何人都愿意在这里提供改进的、更实用的解决方案。
// Extract the PK values from a dictionary and create a key set from these data values
// The expected result here is: set ["PK1"; "PK2"; "PK3"; "PK4"]
let get_keyset (tseq:seq<Dictionary<string,string>>) =
let mutable setres =[]
for x in tseq do
for KeyValue(k,v) in x do
// Extract ID values/keys from each dict
setres <- x.Item("ID")::setres
setres |> List.rev |> Set.ofList
// Sample Data
// First Tuple is PK/ID value
let tabledata = [
[("ID", "PK1"); ("a2","aa"); ("a3", "aaa"); ("a4", "aaaa") ]
[("ID", "PK2"); ("b2","bb"); ("b3", "bbb"); ("b4", "bbbb") ]
[("ID", "PK3"); ("c2","cc"); ("c3", "ccc"); ("c4", "cccc") ]
[("ID", "PK4"); ("d2","dd"); ("d3", "ddd"); ("d4", "dddd") ]
]
//generate dict sequence from datasets
let gendictseq tabledata =
seq {
for tl in tabledata do
let ddict = new Dictionary<string,string>()
for (k,v) in tl do
ddict.Add(k,v)
yield ddict
}
I wrote the following code to extract the values of the ID fields in a sequence of dictionaries
and return these as a set - basically I'm looking for the PK values for rows in a db table that
I've used to populate a dictionary (1 dict per table row). Below the code is some sample data
that is loaded into the dictionary sequences.
While the code below works the style probably could be more functional - I had to resort to
using a mutable list to build the result set. So it does not feel right to me.
Anyone care to offer an improved, more functional solution here.
// Extract the PK values from a dictionary and create a key set from these data values
// The expected result here is: set ["PK1"; "PK2"; "PK3"; "PK4"]
let get_keyset (tseq:seq<Dictionary<string,string>>) =
let mutable setres =[]
for x in tseq do
for KeyValue(k,v) in x do
// Extract ID values/keys from each dict
setres <- x.Item("ID")::setres
setres |> List.rev |> Set.ofList
// Sample Data
// First Tuple is PK/ID value
let tabledata = [
[("ID", "PK1"); ("a2","aa"); ("a3", "aaa"); ("a4", "aaaa") ]
[("ID", "PK2"); ("b2","bb"); ("b3", "bbb"); ("b4", "bbbb") ]
[("ID", "PK3"); ("c2","cc"); ("c3", "ccc"); ("c4", "cccc") ]
[("ID", "PK4"); ("d2","dd"); ("d3", "ddd"); ("d4", "dddd") ]
]
//generate dict sequence from datasets
let gendictseq tabledata =
seq {
for tl in tabledata do
let ddict = new Dictionary<string,string>()
for (k,v) in tl do
ddict.Add(k,v)
yield ddict
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的
get_keyset
对我来说看起来很复杂。这要简洁得多:对于 gendictseq ,我个人更喜欢高阶函数而不是序列表达式,但这在很大程度上是一个品味问题:
Your
get_keyset
looks quite convoluted to me. This is considerably more succinct:For
gendictseq
, I would personally prefer higher-order functions over a sequence expression, but that is largely a matter of taste:使用
ResizeArray
(C# 中的List
)比可变列表变量更好:或者使用功能更强大的序列计算:
Using a
ResizeArray
(List<T>
in C#) is better than a mutable list variable:or use the more functional sequence computation:
从功能上讲,该操作的映射如下所示:
Functionally this operation is map as shown below: