c# - Update values in ConcurrentDictionary>
问题描述
Based on ConcurrentDictionary<string, Tuple<string, string>>
, I need to update Tuple.item1 string to remove spaces.
what I have tried so far :
ConcurrentDictionary<string, Tuple<string, string>> myDictionary = new <string, Tuple<string, string>>
RemoveSpacesFromDic(myDictionary);
public Boolean ShouldRemoveSpace(string myValue)
{
return myValue.Contains(" ");
}
public void RemoveSpacesFromDic(ConcurrentDictionary<string, Tuple<string, string>> sampleDictionary)
{
List<string> keys = new List<string>(sampleDictionary.Keys);
foreach (string key in keys)
{
if (ShouldRemoveSpace(sampleDictionary[key].Item1))
{
string newValue= sampleDictionary[key].Item1;
//Remove spaces from newValue logic
sampleDictionary[key] = new Tuple<string, string>(newValue, sampleDictionary[key].Item2);
}
}
}
Is there an elegant way to do that without the List of keys logic? with LINQ maybe.
解决方案
以下是使用 LINQ 的方法:
yourDic.ToDictionary(x => x.Key,
x => Tuple.Create(x.Value.Item1.Replace(" ", ""), x.Value.Item2));
推荐阅读
- java - 打印二维阵列的方法
- android - 从活动转到主要活动并再次返回活动后,音高检测 (TarsosDSP) 不起作用
- ios - 如何在 Xcode 的 Storyboard 中保持高分辨率图像而不使其像素化?
- c# - 调用 JsonConvert.SerializeObject 时如何转换编码的 JSON 字符串属性?
- javascript - 低效的正则表达式?
- scala - Scala/Spark:如何根据常用列进行外连接
- python - 如何中止创建的协程python
- c# - 如何在不使用 System.Management 的情况下在 Unity 中的 Windows 上查找可用设备及其 COM 端口?
- c++ - C++ decltype 解释
- python - 序列化和反序列化来自用户定义类的对象