48422

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

  • pow function and long int causing problems
  • NSData with CCCrypt in multi-threaded environment
  • Different browser means a different client from the same machine
  • Encryption code in Java
  • FolderBrowserDialog crashes the application
  • C# RSA Encrypting text using a given PKCS#1 public key
  • Securely storing (encrypting) data in an ASP.Net application
  • ScraperWiki: How to create and add records with autoincrement key
  • Scroll down whole browser window
  • time_t conversion format question
  • Sorting a HashMap, while keeping duplicates
  • LINQ to Entities does not recognize the method 'System.String ToString()' method, and this
  • How to apply async task into this
  • Maven use Encrypted passwords in POM
  • VB.NET: How do I use coalesce with db column values and nullable types? Or is there a better solutio
  • WCF service runs in Debug mode but not in Release
  • Change URL After Action is Hit MVC?
  • Emit new line in Inno Setup preprocessor
  • Spring Security bcrypt encoding login is not working
  • How to make SASS put relative paths in its output
  • How to enable mapping the private property of the entity
  • Cypher - matching two different possible paths and return both
  • drawing random circles, storing their coorindates in an array
  • Exception creating JSON with LINQ
  • Primefaces ManyCheckbox inside ui:repeat calls setter method only for last loop
  • Graphics.CopyFromScreen [Web application] + The handle is invalid
  • Adding elements to a huge XML file
  • Object and struct member access and address offset calculation
  • ListItem.Attributes.Add not working
  • Sort List of Strings By Version
  • How can I sort a a table with VBA with given text condition?
  • script to move all files from one location to another location
  • php design question - will a Helper help here?
  • Understanding cpu registers
  • How do I configure my settings file to work with unit tests?
  • Turn off referential integrity in Derby? is it possible?
  • Add sale price programmatically to product variations
  • Unable to use reactive element in my shiny app
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How do I use LINQ to get all the Items that have a particular SubItem?