更新 ConcurrentDictionary<string, Tuple<string, string>> 中的值

ELB*_*oTn 2 c# linq dictionary tuples

基于ConcurrentDictionary<string, Tuple<string, string>>,我需要更新 Tuple.item1 字符串以删除空格。

到目前为止我尝试过的:

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);
      }
    }
}
Run Code Online (Sandbox Code Playgroud)

如果没有键列表逻辑,有没有一种优雅的方法来做到这一点?也许用 LINQ。

Sel*_*enç 5

以下是使用 LINQ 的方法:

yourDic.ToDictionary(x => x.Key,                    
                     x => Tuple.Create(x.Value.Item1.Replace(" ", ""), x.Value.Item2));
Run Code Online (Sandbox Code Playgroud)