c# - 如何在 C# 中将字典(以数组作为值)转换为列表

标签 c# arrays list dictionary

我需要转换 dictionary变成 list这样我就可以将它绑定(bind)到网格。

我的 dictionarystring作为 关键 , 和 arraydoubles作为 值(value) .

我可以转换 dictionarylist通过使用 ToList<> dictionary 上的功能,但是当我将它绑定(bind)到网格时,array在网格列中显示为对象。

如何在 array 中添加值的 doubleslist ?有没有办法让我提取 array在转换 dictionary 的同时将值放入列表中到 list ?

这就是我目前正在做的事情:

List<KeyValuePair<string, double[]>> dataList = 
                    dataDictionary.ToList<KeyValuePair<string, double[]>>();

我需要保留 stringdouble array 中的值( double 数组的大小为 2)在网格中显示。

我看过这个,但无法满足我的需要:
Convert dictionary to List<KeyValuePair>

任何建议或帮助将不胜感激。

提前致谢,

马旺

PS - 我也考虑过转换 dictionarydatatable这样我就可以将它绑定(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/

相关文章:

css - CSS 中的 HTML5 移动列表

c# - 从 Android 中的 ASPX 页面获取响应

C#:如何检查打开的文件是否已更新

c++ - 使用 BigInteger 数组时出现 Malloc 错误

javascript - Lodash,检查对象是否存在以及属性是否存在且为 true

python - 如何从Python中的2D列表中获取最大x和最小y坐标?

c# - MailKit:邮件客户端显示移动时收到的邮件,并且不保留原始标志

c# - 测量查询的代码性能,如何看待结果?

c - 字符串数组 C 的段错误

python - 在给定条件的情况下查找列表的最小和最大索引