更新 ConcurrentDictionary<string, Tuple<string

基于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);

      }

    }

}

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


繁华开满天机
浏览 260回答 1
1回答

千万里不及你

以下是使用 LINQ 的方法:yourDic.ToDictionary(x&nbsp;=>&nbsp;x.Key,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x&nbsp;=>&nbsp;Tuple.Create(x.Value.Item1.Replace("&nbsp;",&nbsp;""),&nbsp;x.Value.Item2));
打开App,查看更多内容
随时随地看视频慕课网APP