Linq in selecting item from ListViewItemCollections in c#

Viewed 17318

how to select a ListViewItem from ListViewItemCollections using Linq in C#?

i tried on using this but it didn't work..

ListViewItemCollections lv = listview1.items;

var test = from xxx in lv where xxx.text = "data 1" select xxx;


test   <--- now has the listviewitem with "data 1" as string value..
4 Answers

If you want to limit your search to just one column you can do so with

IEnumerable<ListViewItem> lv = listView.Items.Cast<ListViewItem>();

var rows =  from x in lv
            where x.SubItems[columnIndex].Text == searchTerm
            select x;

if (rows.Count() > 0)
{
    ListViewItem row = rows.FirstOrDefault();

    //TODO with your row
}
Related