ElektroStudios
ElektroStudios

Reputation: 20464

Listview items to list(Of string)

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

Answers (2)

Khan
Khan

Reputation: 18152

You can use Cast<T>:

List.AddRange(ListView.Items.Cast<WhatTypeAmI>());

Upvotes: 1

SLaks
SLaks

Reputation: 887413

You can use LINQ:

list.AddRange(listView.Items.Cast<ListViewItem>().Select(lvi => lvi.Text));

Upvotes: 6

Related Questions