Reputation: 20464
In C# or VB how can I add a listview items collection to a list but without iterating it using a loop?
The reason is I want to improve this:
For Each Item As ListViewItem In ListView.Items
List.Add(Item)
Next
To something like this else:
List.AddRange(DirectCast(ListView.Items, ...))
Upvotes: 1
Views: 2734
Reputation: 18152
You can use Cast<T>
:
List.AddRange(ListView.Items.Cast<WhatTypeAmI>());
Upvotes: 1
Reputation: 887413
You can use LINQ:
list.AddRange(listView.Items.Cast<ListViewItem>().Select(lvi => lvi.Text));
Upvotes: 6