How do I use LINQ to get all the Items that have a particular SubItem?

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.

Answer1:

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

Answer2:

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.

人吐槽 人点赞

Recommend

Comment

用户名: 密码:
验证码: 匿名发表

你可以使用这些语言

查看评论:How do I use LINQ to get all the Items that have a particular SubItem?