从Dictionary<double, List<double>>
到Dictionary<double, List<double?>>
的最佳方法是什么?以下是我到目前为止所采取的步骤:
1-对于List<double>
到List<double?>
,以下方法有效(不确定这是否是最有效的方法):List<double?> newDic = oldDic.Select(s => (double?) s).ToList();
2-对于Dictionary会话,我尝试过:Dictionary <double, List<double?>> newDic = oldDic.Keys.Select(key => new { Key = key, Value = new List<double?>() { oldDic[key].Select(s => (double?) s).ToList()} }).ToDictionary(grp => grp.Key, grp => grp.Value);
这行不通,我不知道如何让它工作。谢谢大家的意见。
您可以直接调用ToDictionary
。不需要创建中间匿名类型:
oldDict.ToDictionary(
x => x.Key, // x here is already a KeyValuePair<double, List<double>>
// you already know how to convert a List<double> to List<double?>,
// do it to x.Value here:
x => x.Value.Select(y => (double?)y).ToList()
)