C#检查列表视图Windows窗体中是否存在数据

时间:2017-09-14 03:23:06

标签: c# winforms

我想要做的是实际获取一组数据,然后按标识符对其进行分组,如下所示:

123456 123456 456789 456789 456789 135790

Name       Quantity
123456        2
456789        3 
135790        1

到目前为止我做了什么:

Foreach(string name in itemlist) //itemlist = 123456,123456... as mentioned above
{
  var listitems= lvTest.Items.Cast<ListViewItem>;
  bool exists = listitems.Where(item => item.Text == name).Any(); // to check if item name is already exists in list view
  if (!exists)
  {
        ListViewItem lvItem = new ListViewItem(new string[] { name, "1" });
        lvTest.Items.Add(lvItem);
   }
        else
   {
        ListViewItem lvItem = lvTest.Items.Cast<ListViewItem>.Where(item => item.Text == name).FirstOrDefault();
        int count = (int)lvItem.SubItems[1].Text;
        count = count + 1;
        lvItem.SubItems[1].Text = count.ToString();
   }
}

但由于问题而无法工作&#34;无法将方法组分配给隐式类型的局部变量&#34;在

var listitems= lvTest.Items.Cast<ListViewItem>

请提前帮助和谢谢。

2 个答案:

答案 0 :(得分:1)

天哪,最后的问题是因为我没有在Cast函数后放括号。它应该是

var ex = {
    category: {} // or you can initialize `name` here as well
} as example;
ex.category.name = 'electric'; 

答案 1 :(得分:1)

为什么不使用这样的东西,

List<string> lstString = new List<string> { "123456", "123456", "456789", "456789", "456789", "135790" };

var lstGroupList = lstString .GroupBy(item => item,
              (key, group) => new { key, Items = group.ToList()}).ToList();