有沒有辦法將listView.SelectedIndices
轉換爲List<int>
?
我想這將listView.SelectedIndices轉換爲列表<int>
(List<int>)reListViewAllMovies.SelectedIndices.Cast<List<int>>()
但它不工作(InvalidCastException的)。
如果解決方案不存在,是否有任何「單行」解決方案,例如使用lambda表達式?
有沒有辦法將listView.SelectedIndices
轉換爲List<int>
?
我想這將listView.SelectedIndices轉換爲列表<int>
(List<int>)reListViewAllMovies.SelectedIndices.Cast<List<int>>()
但它不工作(InvalidCastException的)。
如果解決方案不存在,是否有任何「單行」解決方案,例如使用lambda表達式?
老問題,但我一直在尋找一個解決方案自己。 .NET v4.5。我不知道早期版本的框架。
var list = myListView.SelectedIndices.Cast<int>().ToList();
是的,這就是我正在尋找的,謝謝。也適用於.NET 4.0。 – icl7126
reListViewAllMovies.SelectedIndices.Select(i => (int)i).ToList();
或者,你可以使用一個foreach
循環轉換的結果:
var newList = new List<int>();
foreach(int i in reListViewAllMovies.SelectedIndices)
{
newList.Add(i);
}
我編輯了您的標題。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 –