1

我需要将 a 转换dictionary为 alist以便可以将其绑定到网格。

My dictionaryhasstring作为, an arrayofdoubles作为

我可以使用 上的函数将 转换dictionary为 a ,但是当我将其绑定到网格时,会在网格列中显示为对象。listToList<>dictionaryarray

如何将 of 中的值array添加doubleslist?有没有办法让我在将值转换为 aarray的同时将值提取到列表中?dictionarylist

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

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

我需要保留string和 中的两个doublearray(双精度数组的大小为 2)以显示在网格中。

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

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

提前致谢,

马旺

PS - 我也考虑过将其转换dictionary为 adatatable以便我可以将其绑定到网格,但我不确定这是否可行。

4

5 回答 5

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

我需要保留数组中的字符串和两个双精度值(双精度数组的大小为 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();
于 2012-08-16T05:52:23.810 回答
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;
        }

希望这可以帮助

于 2012-08-16T10:10:46.880 回答
1

您可以为此使用元组:

var result = dataDictionary.Select(x => Tuple.Create(x.Key, x.Value[0], x.Value[1]))
                           .ToList();
于 2012-08-16T07:02:52.750 回答
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();
于 2012-08-16T06:11:33.270 回答
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;
}

现在您可以将 Grid 绑定到List<HelperClass>

于 2012-08-16T06:11:42.633 回答