1

I need to convert a dictionary into a list so that I can bind it to a grid.

My dictionary has string as a key, and an array of doubles as a value.

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.

How can I add the values in the array of doubles to the 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?

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.

I have looked at this, but couldn't implement it for what I need: Convert dictionary to 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.

Community
  • 1
  • 1
Marwan مروان
  • 2,163
  • 8
  • 30
  • 40
  • 3
    It's not quite clear for me what you want to achieve. Your question reads as if you want the keys and values be mixed together in one list. Binding it to a grid would result in one column with keys and values in it. That doesn't seem to make too much sense. Maybe you can elaborate a bit more? – Daniel Hilgarth Aug 16 '12 at 06:01
  • @DanielHilgarth - Hi Daniel. I'm trying to have each key (string) and value (double[0] and double[1]) to display in its own row but in separate columns. I hope that helps clarify it a bit :-) In the code that I have above, I can to convert a `dictionary` to a `list`. When I bind the `list` to the grid, it shows the **key** (string) in a column, and the **value** (double array of size two) is in another column, but it is displayed as an array object (values are not visible). I need the values from the array to be displayed in their own columns. – Marwan مروان Aug 16 '12 at 07:00

5 Answers5

3
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.

You should implement additional logic that will iterate thru array on the view. Also you can create Tuple<string,double,double> and bind 2 double values manually.

            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();
user854301
  • 5,383
  • 3
  • 28
  • 37
2

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

Niladri Biswas
  • 4,153
  • 2
  • 17
  • 24
1

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();
logicnp
  • 5,796
  • 1
  • 28
  • 32
1

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>

Jürgen Steinblock
  • 30,746
  • 24
  • 119
  • 189
1

You can use a Tuple for this:

var result = dataDictionary.Select(x => Tuple.Create(x.Key, x.Value[0], x.Value[1]))
                           .ToList();
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443