将listView.SelectedIndices转换为List <int>

icl*_*126 5 c# listview casting

有没有办法投listView.SelectedIndicesList<int>
我试过这个

(List<int>)reListViewAllMovies.SelectedIndices.Cast<List<int>>()
Run Code Online (Sandbox Code Playgroud)

但它不起作用(InvalidCastException).
如果解决方案不存在,是否有任何"单行"解决方案,例如使用lambda表达式?

and*_*eer 8

老问题,但我一直在寻找解决方案..NET v4.5.我不知道该框架的早期版本.

var list = myListView.SelectedIndices.Cast<int>().ToList();
Run Code Online (Sandbox Code Playgroud)


Jus*_*ner 5

reListViewAllMovies.SelectedIndices.Select(i => (int)i).ToList();
Run Code Online (Sandbox Code Playgroud)

或者,您可以使用foreach循环来转换结果:

var newList = new List<int>();
foreach(int i in reListViewAllMovies.SelectedIndices)
{
    newList.Add(i);
}
Run Code Online (Sandbox Code Playgroud)

  • `myListView.SelectedIndices.Cast <int>的()ToList();` (2认同)