2012-08-16 72 views
1

我需要将dictionary转换为list,以便我可以将它绑定到网格。如何将字典(以数组作为值)转换为C列表#

dictionary具有string关键,以及doublesarray

我可以通过使用关于dictionaryToList<>函数转换dictionarylist,但是当我其绑定到网格,array被显示为在网格列的对象。

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

这是目前我在做什么:

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

我需要保存string两者doublearray(双打的数组大小两种)在网格中显示。

我已经看过这个,但无法实现它为我所需要的: Convert dictionary to List<KeyValuePair>

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

由于提前,

马尔

PS - 我也想过转换dictionarydatatable,这样我可以把它绑定到网格,但我不知道这是可行的。

+3

这不是我很清楚自己想要达到的目标。你的问题就像你想把键和值混合在一个列表中一样。将它绑定到网格会导致一列中包含键和值。这似乎没有太大意义。也许你可以详细阐述一下? – 2012-08-16 06:01:22

+0

@DanielHilgarth - 嗨丹尼尔。我试图让每个键(字符串)和值(double [0]和double [1])显示在其自己的行中,但在单独的列中。我希望这有助于澄清它:-) 在上面的代码中,我可以将一个'dictionary'转换为'list'。当我将'list'绑定到网格时,它将在列中显示**键**(字符串),并且**值**(大小为2的双数组)会出现在另一列中,但显示为一个数组对象(值不可见)。 我需要将数组中的值显示在自己的列中。 – 2012-08-16 07:00:15

+0

请参阅下面的答案。 – 2012-08-16 07:03:34

回答

2

字典到列表

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; 
     } 

希望这有助于

3
IList<double> list = dictionary.SelectMany(item => item.Value).ToList(); 

我需要保留在数组中的串和两个双值( 阵列双打的大小是两个)在网格显示。

您应该实现额外的逻辑,该逻辑将在视图上迭代到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(); 
1

试试这个:

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(); 
1

这个怎么样

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>

1

你可以使用一个元组这样的:

var result = dataDictionary.Select(x => Tuple.Create(x.Key, x.Value[0], x.Value[1])) 
          .ToList();