[英]How to Convert Dictionary (with Array as Value) to List in C#
I need to convert a dictionary
into a list
so that I can bind it to a grid. 我需要将
dictionary
转换为list
以便将其绑定到网格。
My dictionary
has string
as a key , and an array
of doubles
as a value . 我的
dictionary
有string
作为重点 ,以及array
的doubles
作为值 。
I can convert the dictionary
to a list
by using the ToList<>
function on the dictionary
, but when I bind it to the grid, the array
is displayed as an object in the grid column. 我可以转换
dictionary
为list
通过使用ToList<>
在功能dictionary
,但是当我其绑定到网格, array
被显示为在网格列的对象。
How can I add the values in the array
of doubles
to the list
? 如何将
doubles
array
中的值添加到list
? Is there a way for me to extract the array
values into the list at the same time as I'm converting the dictionary
to a list
? 在将
dictionary
转换为列表的同时,是否可以将array
值提取到list
?
This is what I'm currently doing: 这是我目前正在做的:
List<KeyValuePair<string, double[]>> dataList =
dataDictionary.ToList<KeyValuePair<string, double[]>>();
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. 我需要将
string
和两个double
值保留在array
( double
数组的大小为2)以显示在网格中。
I have looked at this, but couldn't implement it for what I need: Convert dictionary to List<KeyValuePair> 我已经看过了,但是无法满足我的需要: 将字典转换为List <KeyValuePair>
Any suggestions or help would be greatly appreciated. 任何建议或帮助将不胜感激。
Thanks in advance, 提前致谢,
Marwan 马万
PS - I have also thought about converting the dictionary
to a datatable
so that I can bind it to the grid, but I'm not sure if that's doable. PS -我也想过转换
dictionary
的datatable
,这样我可以把它绑定到网格,但我不知道这是可行的。
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.
我需要将字符串和两个double值保留在数组中(double数组的大小为2)以显示在网格中。
You should implement additional logic that will iterate thru array
on the view. 您应该实现其他逻辑,该逻辑将遍历视图上的
array
。 Also you can create Tuple<string,double,double>
and bind 2 double
values manually. 您也可以创建
Tuple<string,double,double>
并手动绑定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();
Dictionary To List 字典列表
Dictionary<string, double[]> dict = new Dictionary<string, double[]>();
dict.Add("1", new double[] { 1.10, 1.20 });
dict.Add("2", new double[] { 2.10, 2.20 });
dict.Add("3", new double[] { 3.10, 3.20 });
dict.Add("4", new double[] { 4.10, 4.20 });
dict.Add("5", new double[] { 5.10, 5.20 });
var lstRecord = dict.Select(i => new { Key = i.Key, Val1 = i.Value[0], Val2 = i.Value[1] }).ToList();
dataGridView1.DataSource = lstRecord;
Dictionary To Datatable 字典到数据表
private void button1_Click(object sender, EventArgs e)
{
Dictionary<string, double[]> dict = new Dictionary<string, double[]>();
dict.Add("1", new double[] { 1.10, 1.20 });
dict.Add("2", new double[] { 2.10, 2.20 });
dict.Add("3", new double[] { 3.10, 3.20 });
dict.Add("4", new double[] { 4.10, 4.20 });
dict.Add("5", new double[] { 5.10, 5.20 });
dataGridView1.DataSource = Dictionary2Table(dict);
}
public DataTable Dictionary2Table(Dictionary<string, double[]> dict)
{
DataTable dt = new DataTable();
dt.Columns.Add("key", typeof(string));
dt.Columns.Add("Value1", typeof(double));
dt.Columns.Add("Value2", typeof(double));
dict
.Select(i => new { Key = i.Key, Val1 = i.Value[0], Val2 = i.Value[1] })
.ToList()
.ForEach(i=>dt.Rows.Add(i.Key,i.Val1,i.Val2));
return dt;
}
Hope this helps 希望这可以帮助
Try this: 尝试这个:
Dictionary<string, double[]> dict = new Dictionary<string, double[]>()
{
{"a",new double[]{1,2}},
{"b",new double[]{3,4}}
};
List<Tuple<string, double, double>> list = dict.Select(kvp => Tuple.Create(kvp.Key, kvp.Value[0], kvp.Value[1])).ToList();
What about this 那这个呢
public class HelperClass
{
private readonly KeyValuePair<string, double[]> Item;
[Bindable]
public string Key { get { return Item.Key; } }
[Bindable]
public double Value1 {get { return Item.Value[0]; } }
[Bindable]
public double Value2 {get { return Item.Value[1]; } }
public HelperClass(KeyValuePair<string, double[]> item)
{
this.Item = item;
}
}
public List<HelperClass> Convert(Dictionary<string, double[]> items)
{
var result = new List<HelperClass>(items.Count);
foreach(var item in items)
result.Add(new HelperClass(item));
return result;
}
Now you can bind your Grid to the List<HelperClass>
现在,您可以将网格绑定到
List<HelperClass>
You can use a Tuple for this: 您可以为此使用元组:
var result = dataDictionary.Select(x => Tuple.Create(x.Key, x.Value[0], x.Value[1]))
.ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.