基于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。
1 回答
千万里不及你
TA贡献1784条经验 获得超9个赞
以下是使用 LINQ 的方法:
yourDic.ToDictionary(x => x.Key, x => Tuple.Create(x.Value.Item1.Replace(" ", ""), x.Value.Item2));
- 1 回答
- 0 关注
- 258 浏览
添加回答
举报
0/150
提交
取消