如何将 List<Dictionary<string, byte[]> 对象添加到 Dictionary<string, byte[]>
How to add List<Dictionary<string, byte[]> object to Dictionary<string, byte[]>
如何将 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[]>>
}
尝试像下面这样修改循环:
public static async void PostUploadtoCloud(List<Dictionary<string, byte[]>> _commonFileCollection)
{
Dictionary<string, Byte[]> _dickeyValuePairs = new Dictionary<string, byte[]>();
Byte[] itemData;
foreach (var item in _commonFileCollection)
{
foreach (var kvp in item)
{
if (!_dickeyValuePairs.TryGetValue(kvp.Key, out itemData))
{
_dickeyValuePairs.Add(kvp.Key, kvp.Value);
}
}
}
}
更新:
外循环将遍历列表中的每个字典,而内循环将遍历字典的每个项目。附加部分 _dickeyValuePairs.TryGetValue
将帮助您避免在添加重复键时出现异常。
执行此操作时,您需要在代码中采用一些安全措施,@Pritish 所说的简单合并由于可能的异常而无法工作,
public static async void PostUploadtoCloud(List<Dictionary<string, byte[]>> _commonFileCollection)
{
Dictionary<string, Byte[]> _dickeyValuePairs = new Dictionary<string, byte[]>();
try
{
foreach (var item in _commonFileCollection)
{
foreach (var kvp in item)
{
//you can also use TryAdd
if(!_dickeyValuePairs.Contains(kvp.Key))
{
_dickeyValuePairs.Add(kvp.Key, kvp.Value);
}
else
{
//send message that it could not be done?
}
}
}
}
catch(Exception e)
{
//log exception
}
}
如何将 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[]>>
}
尝试像下面这样修改循环:
public static async void PostUploadtoCloud(List<Dictionary<string, byte[]>> _commonFileCollection)
{
Dictionary<string, Byte[]> _dickeyValuePairs = new Dictionary<string, byte[]>();
Byte[] itemData;
foreach (var item in _commonFileCollection)
{
foreach (var kvp in item)
{
if (!_dickeyValuePairs.TryGetValue(kvp.Key, out itemData))
{
_dickeyValuePairs.Add(kvp.Key, kvp.Value);
}
}
}
}
更新:
外循环将遍历列表中的每个字典,而内循环将遍历字典的每个项目。附加部分 _dickeyValuePairs.TryGetValue
将帮助您避免在添加重复键时出现异常。
执行此操作时,您需要在代码中采用一些安全措施,@Pritish 所说的简单合并由于可能的异常而无法工作,
public static async void PostUploadtoCloud(List<Dictionary<string, byte[]>> _commonFileCollection)
{
Dictionary<string, Byte[]> _dickeyValuePairs = new Dictionary<string, byte[]>();
try
{
foreach (var item in _commonFileCollection)
{
foreach (var kvp in item)
{
//you can also use TryAdd
if(!_dickeyValuePairs.Contains(kvp.Key))
{
_dickeyValuePairs.Add(kvp.Key, kvp.Value);
}
else
{
//send message that it could not be done?
}
}
}
}
catch(Exception e)
{
//log exception
}
}