我有一个我不满意的方法,你能告诉我如何做得更好吗?
public Foo WithBar(IDictionary<string, object> parameters) {
var strStrDict = new Dictionary<string, string>(parameters.Count);
foreach(var pair in parameters)
{
strStrDict.Add(pair.Key, pair.Value != null ? pair.Value.ToString() : (string)null);
}
// Call overload which takes IDictionary<string, string>
return this.WithBar(strStrDict);
}
此代码有效,但我确信我缺少一种很好的 linq'y 方法。
最佳答案
parameters.ToDictionary(k=>k.Key, v=>v.Value!=null?v.Value.ToString():(string)null);
关于c# - 什么是将 IDictionary<string, object> 转换为 IDictionary<string, string> 的优雅方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5736704/