2012-08-16 68 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();