IEnumerable for Listview

I want to write a method to filter the browse list

IEnumerable<ListViewItem> CurrentList;
CurrentList = ListViewExemple.Items.Cast<ListViewItem>(); 
var result = CurrentList.Select(i => i.Text.Contains(SearchTxtBox.Text));


Now How can I add result items to the cleaned up Listview


source to share

2 answers

You need to keep in mind that the ListViewItem has an owner, you need to make a copy of the item in order to put it in another list. Ease of use of the Clone () method. Or move it from one to the other, unlikely in this case. So you probably want this:

    var matches = listView1.Items.Cast<ListViewItem>()
                  .Select(item => (ListViewItem)item.Clone())
                  .Where(item => item.Text.Contains(SearchTxtBox.Text));




Unfortunately, it ListView

does not have the ability to bind data. Therefore, you need to add items manually:

foreach (var item in result)


Or perhaps:



Also, you seem to have an error:

CurrentList.Select(i => i.Text.Contains(SearchTxtBox.Text))


This will not create collection ListViewItem

s, and it will create collection bool

s. I think you meant this:

CurrentList.Where(i => i.Text.Contains(SearchTxtBox.Text))




All Articles