C# 无法从lsitviewbox中删除项目

C# 无法从lsitviewbox中删除项目,c#,C#,在C#windows应用程序中,我比较两个不同的字符串数组,并根据数组的大小,向列表视图框添加或删除项目。使用下面的代码,我可以添加到列表视图而没有任何问题,但我无法从中删除 我得到一个错误,上面写着 错误CS1503,参数1:无法从“字符串”转换为“System.Windows.Forms.ListViewItem” 这是我代码的一个摘录 string[] currentFilesList = GetFileList(); if (currentFilesL

在C#windows应用程序中,我比较两个不同的字符串数组,并根据数组的大小,向列表视图框添加或删除项目。使用下面的代码,我可以添加到列表视图而没有任何问题,但我无法从中删除

我得到一个错误,上面写着

错误CS1503,参数1:无法从“字符串”转换为“System.Windows.Forms.ListViewItem”

这是我代码的一个摘录

        string[] currentFilesList = GetFileList();  
        if (currentFilesList.Length > prevFilesList.Length)
        {
            var addedList = currentFilesList.Except(prevFilesList).ToArray();
            foreach (var item in addedList)
            {
                listView1.Items.Add(item);
            }
        }
        if (currentFilesList.Length < prevFilesList.Length)
        {
            var removedList = prevFilesList.Except(currentFilesList).ToArray();
            foreach (string item in removedList)
            {                   
                    listView1.Items.Remove(item);    //I get error here on "item" Argument 1: cannot convert from 'string' to 'System.Windows.Forms.ListViewItem'"                    
            }
        }
        prevFilesList = currentFilesList;
string[]currentFileList=GetFileList();
如果(CurrentFileList.Length>PrevFileList.Length)
{
var addedList=currentFilesList.Except(prevFilesList.ToArray();
foreach(添加列表中的var项)
{
listView1.Items.Add(项目);
}
}
if(CurrentFileList.Length

我尝试了字符串和变量,但结果相同。

您可以通过

  foreach (string item in removedList)
{
        var toRemove =listView1.Items.Find(item);
        if (toRemove != null)
        {
           listView1.Items.Remove(toRemove);
        }
}
或者您可以使用
RemoveByKey

foreach (string item in removedList)
    {

               listView1.Items.RemoveByKey(item);

    }

您可以使用linq来尝试这一点

var newlist = listView1.Cast<ListViewItem>().Where(p=>p.Text.Contains("OBJECT")).ToList().ForEach(listBox1.Items.Remove);
var newlist=listView1.Cast().Where(p=>p.Text.Contains(“OBJECT”)).ToList().ForEach(listBox1.Items.Remove);