[英]How to Convert Dictionary (with Array as Value) to List in C#
我需要将dictionary
转换为list
以便将其绑定到网格。
我的dictionary
有string
作为重点 ,以及array
的doubles
作为值 。
我可以转换dictionary
为list
通过使用ToList<>
在功能dictionary
,但是当我其绑定到网格, array
被显示为在网格列的对象。
如何将doubles
array
中的值添加到list
? 在将dictionary
转换为列表的同时,是否可以将array
值提取到list
?
这是我目前正在做的:
List<KeyValuePair<string, double[]>> dataList =
dataDictionary.ToList<KeyValuePair<string, double[]>>();
我需要将string
和两个double
值保留在array
( double
数组的大小为2)以显示在网格中。
我已经看过了,但是无法满足我的需要: 将字典转换为List <KeyValuePair>
任何建议或帮助将不胜感激。
提前致谢,
马万
PS -我也想过转换dictionary
的datatable
,这样我可以把它绑定到网格,但我不知道这是可行的。
IList<double> list = dictionary.SelectMany(item => item.Value).ToList();
我需要将字符串和两个double值保留在数组中(double数组的大小为2)以显示在网格中。
您应该实现其他逻辑,该逻辑将遍历视图上的array
。 您也可以创建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<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;
字典到数据表
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;
}
希望这可以帮助
尝试这个:
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();
那这个呢
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;
}
现在,您可以将网格绑定到List<HelperClass>
您可以为此使用元组:
var result = dataDictionary.Select(x => Tuple.Create(x.Key, x.Value[0], x.Value[1]))
.ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.