views:

44

answers:

1

The list view is like

alt text

i need to add the data from the Listview to a Dictionary<String,String>.

Now i'm using for loop to do this. Is there any way to do this using LINQ.

Finally the Dictionary will contain

{"A","1"}
{"B","2"}
{"C","3"}

EDIT My code :

        Dictionary<String, String> Dic = new Dictionary<string, string>();
        for (int i = 0; i < listView1.Items.Count; i++)
        {
            Dic.Add(listView1.Items[i].Text.ToString(), listView1.Items[i].SubItems[1].Text.ToString());
        }

Thanks in advance

+2  A: 

Based on your sample:

Dictionary<String, String> Dic = listView.Items
    .Cast<ListViewItem>()
    .ToDictionary(x => x.Text, x => x.SubItems[0].Text);
Kirk Woll