开发者

C# : How to add data data from a ListView control to a Dictionary

开发者 https://www.devze.com 2023-01-20 21:56 出处:网络
The list view is like 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 wa开发者_开发百科y to do this using LINQ.

The list view is like

C# : How to add data data from a ListView control to a Dictionary

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 wa开发者_开发百科y 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


Based on your sample:

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

精彩评论

暂无评论...
验证码 换一张
取 消