Reputation: 45
My software is designed to encrypt and decrypt files. The user loads the files to be processed into a ListView control. In the control, each item is the file path with one subitem, the type of process (ENCRYPT or DECRYPT).
I need to get a list of all ITEMS (the file paths) that have the "ENCRYPT" subitem, preferably with LINQ. Currently, my code looks like this:
Dim enclist As New ArrayList()
For i As Int32 = 0 To (lvwLoad.Items.Count - 1)
If lvwLoad.Items(i).SubItems(1).Text = "ENCRYPT" Then
enclist.Add(lvwLoad.Items.Item(i).Text)
count += 1
End If
Next
I tried this:
Dim list As IEnumerable(Of String) = From item In lvwLoad.Items
Where item.SubItems(1).Text = "ENCRYPT"
But this statement can't access the SubItems()
array. I know there's probably something simple I'm missing, but I can't figure it out.
EDIT: I know I can do this:
Dim enclist As New List(Of String)
For Each item As ListViewItem In lvwLoad.Items
If item.SubItems(1).Text = "ENCRYPT" Then
enclist.Add(item.Text)
End If
Next
But I really want to know how to do this with LINQ.
Upvotes: 2
Views: 2600
Reputation: 27467
Try code below, you need to cast items to ListViewItem
so that you can access SubItems
Dim list = From item In lvwLoad.Items.Cast(Of ListViewItem) () _
Where item.SubItems(1).Text = "ENCRYPT"
Select item.Text
Upvotes: 1
Reputation: 12315
return lvwLoad.Items.Where(item => item.SubItems(1).Text = "ENCRYPT").Select(s => s.Text);
Dim list As IEnumerable(Of String) = From item In lvwLoad.Items
Where item.SubItems(1).Text = "ENCRYPT"
Select item.Text
I hope this will help.
Upvotes: 1