Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cast listView.SelectedIndices to List<int>

Is there any way to cast listView.SelectedIndices to List<int>?
I tried this

(List<int>)reListViewAllMovies.SelectedIndices.Cast<List<int>>()

But it doesn't work (InvalidCastException).
If solution doesn't exist, is there any "one-line" solution for example using lambda expression?

like image 759
icl7126 Avatar asked Dec 26 '12 20:12

icl7126


2 Answers

Old question but I was looking for a solution myself. .NET v4.5. I don't know about earlier versions of the framework.

var list = myListView.SelectedIndices.Cast<int>().ToList();
like image 147
andleer Avatar answered Sep 20 '22 00:09

andleer


reListViewAllMovies.SelectedIndices.Select(i => (int)i).ToList();

Or, you could use a foreach loop to convert the results:

var newList = new List<int>();
foreach(int i in reListViewAllMovies.SelectedIndices)
{
    newList.Add(i);
}
like image 31
Justin Niessner Avatar answered Sep 21 '22 00:09

Justin Niessner