What is the best way to convert from Dictionary<double, List<double>>
to Dictionary<double, List<double?>>
?
Here are the steps I have taken so far:
1-For List<double>
to List<double?>
, the following works (not sure if it is the most effiecient way):
List<double?> newDic = oldDic.Select(s => (double?) s).ToList();
2- And for the Dictionary converstion, I have tried:
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);
This does not work and I do not know how to make it work. I appreciate any comments.
CodePudding user response:
You can call ToDictionary
directly. There is no need to create the intermediate anonymous type:
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()
)