f# - 将字典(从 C# 传递)转换为 F# 中的字符串
问题描述
编辑:它已解决。答案是:
|> Seq.sortBy (fun (KeyValue(k,v)) -> k)
|> Seq.map (fun (KeyValue(k,v)) -> (sprintf "%s:%s" k v))
|> String.concat ","
原始问题:
我正在尝试做这个的 F# 版本:
var postData = "{"
+ string.Join(
",",
sortedParameters.Select(item => $"\"{item.Key}\":\"{item.Value}\"").ToList())
+ "}";
我有以下字典,来自 C#:
{
{ "a", "1" },
{ "c", "3" },
{ "b", "2" },
}
我想对其进行排序,然后输出字符串中的所有值,如下所示:
"a":"1", "b":"2", "c":"3"
所以,我是这样开始的:
let Test (values : Dictionary<string, string>) : string =
|> Seq.sortBy (fun (KeyValue(k,v)) -> k)
|> Seq.iter (fun (KeyValue(k,v)) -> (sprintf "%s:%s" k v))
但最后一部分没有编译:
Compilation error (line 13, col 50): Type mismatch. Expecting a string -> string -> unit but given a string -> string -> string The type 'unit' does not match the type 'string'
Compilation error (line 13, col 60): This expression was expected to have type string but here has type int
我不明白错误信息。我的假设是这会返回一个字符串列表,例如:
{
"a:1", "b:2", "c:3"
}
我必须在下一步中加入一个字符串
显然,它不是那样做的:)
我错过了什么?
解决方案
推荐阅读
- charts - 如何为谷歌反应柱形图添加背景颜色和边框?
- javascript - Javascript 遍历多选下拉菜单并将数据推送到 JSON 对象中
- ios - 为什么分页滚动在 iPhone12 上不起作用(缺口显示屏)
- node.js - 尝试使用 npm install 时,我在不和谐机器人上收到“未定义 msvsVersion”错误
- python - 为什么 Faster-RCNN 预测的类和类分数不对应于检测到的对象的最高分数?
- python - 将作为列表中元素的字符串值相加
- apache - Apache 服务器和 2 个路径
- python - 在远程端口 80 上运行 jupyter lab
- javascript - InvalidSignatureException:凭证的范围应适用于正确的服务:execute-api
- opencv - 从多个 repos 构建 Bazel 包