如何使用LINQ获取具有特定SubItem的所有Item? [英] How do I use LINQ to get all the Items that have a particular SubItem?

查看:140
本文介绍了如何使用LINQ获取具有特定SubItem的所有Item?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的软件旨在加密和解密文件.用户将要处理的文件加载到ListView控件中.在控件中,每一项都是带有一个子项的文件路径,子项是进程的类型(ENCRYPT或DECRYPT).

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).

我需要获取所有具有"ENCRYPT"子项的项目(文件路径)的列表,最好使用LINQ.目前,我的代码如下:

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

我尝试过:

Dim list As IEnumerable(Of String) = From item In lvwLoad.Items 
                                     Where item.SubItems(1).Text = "ENCRYPT"

但是此语句无法访问SubItems()数组.我知道我可能缺少一些简单的东西,但我无法弄清楚.

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.

我知道我可以做到:

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

但是我真的很想知道如何使用LINQ做到这一点.

But I really want to know how to do this with LINQ.

推荐答案

尝试下面的代码,您需要将项目投射到ListViewItem以便可以访问SubItems

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

这篇关于如何使用LINQ获取具有特定SubItem的所有Item?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆