代码之家  ›  专栏  ›  技术社区  ›  Thorin Oakenshield

C#:如何将ListView控件中的数据添加到字典中

  •  0
  • Thorin Oakenshield  · 技术社区  · 14 年前

    列表视图如下

    alt text

    我需要从 Listview 给一个 Dictionary<String,String> .

    for loop 做这个。有什么方法可以用 LINQ .

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

    编辑 我的代码:

            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());
            }
    

    1 回复  |  直到 14 年前
        1
  •  2
  •   Kirk Woll    14 年前

    根据您的样品:

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