我需要转换 dictionary
变成 list
这样我就可以将它绑定(bind)到网格。
我的 dictionary
有 string
作为 关键 , 和 array
的 doubles
作为 值(value) .
我可以转换 dictionary
到 list
通过使用 ToList<>
dictionary
上的功能,但是当我将它绑定(bind)到网格时,array
在网格列中显示为对象。
如何在 array
中添加值的 doubles
到list
?有没有办法让我提取 array
在转换 dictionary
的同时将值放入列表中到 list
?
这就是我目前正在做的事情:
List<KeyValuePair<string, double[]>> dataList =
dataDictionary.ToList<KeyValuePair<string, double[]>>();
我需要保留
string
和double
array
中的值( double 数组的大小为 2)在网格中显示。我看过这个,但无法满足我的需要:
Convert dictionary to List<KeyValuePair>
任何建议或帮助将不胜感激。
提前致谢,
马旺
PS - 我也考虑过转换
dictionary
到 datatable
这样我就可以将它绑定(bind)到网格,但我不确定这是否可行。
最佳答案
IList<double> list = dictionary.SelectMany(item => item.Value).ToList();
I need to preserve the string and both double values in the array (the array of doubles is of size two) to display in the grid.
您应该实现将通过
array
进行迭代的附加逻辑。在 View 上。您也可以创建Tuple<string,double,double>
并绑定(bind) 2 double
手动设置值。 Func<double[], int, double?> safeGet = (array, index) => array.Length - 1 >= index ? (double?)array[index] : null;
var list = dataList.Select(item => Tuple.Create(item.Key, safeGet(item.Value, 0), safeGet(item.Value, 1))).ToList();
关于c# - 如何在 C# 中将字典(以数组作为值)转换为列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11981331/