简体   繁体   中英

Iterate backwards over a selectedListViewItemCollection

I have tried this code (which works for anything that implements IEnumerable, but apparently not a selectedListViewItemCollection)

foreach (ListViewItem item in ((IEnumerable<ListViewItem>)categoryListView.SelectedItems).AsEnumerable().Reverse())
{
    // do something
}

I get an InvalidCastException when this is run.

That's because SelectedItems does not implement generic IEnumerable<ListViewItem> .

Use Cast<T> method instead:

foreach (ListViewItem item in categoryListView.SelectedItems.Cast<ListViewItem>().AsEnumerable().Reverse())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM