c# - 如何添加列表反对字典
问题描述
如何添加List<Dictionary<string, byte[]>
对象Dictionary<string, byte[]>
public static async void PostUploadtoCloud( List<Dictionary<string, byte[]>> _commonFileCollection)
{
Dictionary<string, Byte[]> _dickeyValuePairs = new Dictionary<string, byte[]>();
foreach (var item in _commonFileCollection)
{
_dickeyValuePairs.add(item.key,item.value); // i want this but I am getting
//_dickeyValuePairs.Add(item.Keys, item.Values); so I am not able to add it dictionary local variable _dickeyValuePairs
}
}
在 foreach 循环中我得到了item.KEYS
,item.VALUES
所以我该如何添加它
_dickeyValuePairs
解决方案
如果你想合并它们,那么像这样:
public static async void PostUploadtoCloud( List<Dictionary<string, byte[]>> _commonFileCollection)
{
var _dickeyValuePairs = _commonFileCollection.SelectMany(x=> x).ToDictionary(x=> x.Key, x=> x.Value);
}
但请注意,如果它们包含相同的键 - 你会得到异常。
为避免它-您可以使用查找(基本上是字典,但在值中它存储集合):
public static async void PostUploadtoCloud( List<Dictionary<string, byte[]>> _commonFileCollection)
{
var _dickeyValuePairs = _commonFileCollection.SelectMany(x=> x).ToLookup(x=> x.Key, x=> x.Value); //ILookup<string, IEnumerable<byte[]>>
}
推荐阅读
- javascript - Vue-事件总线内的变量不是反应式的
- javascript - 关于如何在客户端和服务器之间使用 Websockets 的简单示例
- pdfbox - 使用 PdfBox 将 Pdf 转换为图像
- html - 如何在不改变网站响应能力的情况下增加轮播的宽度
- ios - 当我重新启动我的设备时,是否可以在没有任何用户交互的情况下连接蓝牙设备,并在 iOS 后台执行任务?
- java - 在华为中自动将文本粘贴到编辑短信中的文本
- java - 如何从另一个类调用活动的一种方法
- python - PyQT5 program returning error only during first compile run
- react-native - 节点服务器在本机反应中停止
- python - 使用 Python 从网站中提取表数据