Hello all. I have a question on searching a List(Of CLass). I am not sure how it is labeled List<OF T> or List(Of Class) However you label it here is the Class EXT

Public Class EXT
  Implements IComparable

  Private m_extension As String
  Private m_exe As String
  Private m_description As String

  'Public Sub New()
  'End Sub

  Public Sub New(ByVal eExtension As String, _
                 ByVal eDescription As String, ByVal eExecutable As String)

    m_extension = eExtension
    m_description = eDescription
    m_exe = eExecutable

  End Sub

  Public Property EXTENSION() As String
    Get
      Return m_extension.ToLower
    End Get
    Set(ByVal value As String)
      m_extension = value
    End Set
  End Property

  Public Property DESCRIPTION() As String
    Get
      Return m_description
    End Get
    Set(ByVal value As String)
      m_description = value
    End Set
  End Property

  Public Property EXECUTABLE() As String
    Get
      Return m_exe
    End Get
    Set(ByVal value As String)
      m_exe = value
    End Set
  End Property

  Public Function ToArray() As String()
    Dim ret(2) As String
    ret(0) = m_extension
    ret(1) = m_description
    ret(2) = m_exe

    Return ret
  End Function

  Public Function CompareTo(ByVal obj As Object) As Integer _
                                                 Implements System.IComparable.CompareTo

    Dim other As EXT = DirectCast(obj, EXT)
    Return Me.EXTENSION.CompareTo(other.EXTENSION)

  End Function

End Class

Here is a sample the CSV file there are over 936 records.

EXTENSION,DESCRIPTION,EXECUTABLE
.flac,Music,C:\Program Files\JetAudio\jetaudio.exe
.flv,Flash Video file,C:\Program Files\Combined Community Codec Pack\MPC\mpc-hc.exe
.....

I have lstExt which is a List Of EXT. I fill it from a CSV file and display it on a DataGridView through a BindingSource. I have a textbox which the user types in the extension they want to search for and presses the Button. The searched for extension if found is selected and scrolled to in the DataGridView. Now here are the multiple ways in which to search for my extension.

  Private Sub SearchForExtension(ByVal sItem As String)
    If sItem.Length > 2 Then
      'Get Selected Index of user entered file extension

      'LINQ way
      Dim query = From exts In lstExt _
                  Where exts.EXTENSION = sItem _
                  Select exts
      Dim idx1 As Integer = lstExt.IndexOf(CType(query(0), EXT))

      'Way (2)
      Dim foundCustomer As EXT = Nothing
      foundCustomer = lstExt.FirstOrDefault(Function(s) s.EXTENSION = sItem)
      Dim idx As Integer = lstExt.IndexOf(foundCustomer)

      'Way(3)
      Dim sIndex As Integer = lstExt.FindIndex(Function(p As EXT) p.EXTENSION = sItem)

      'Way (4)
      Dim myLocatedObject As EXT = lstExt.Find(Function(p As EXT) p.EXTENSION = sItem)
      Dim myIndex As Integer = lstExt.IndexOf(myLocatedObject)

      If idx > -1 Then
        dgv_csv.ClearSelection()
        dgv_csv.Rows(idx).Selected = True
        dgv_csv.FirstDisplayedScrollingRowIndex = idx
        dgv_csv.PerformLayout()
      End If
    End If


  End Sub

As you can see from the SearchForExtension Sub I have 4 different ways to search for the extension. The index is used to select the searched for row in the DataGridView. My question is which is best/preferred?

Thanks

Hi kRod,

In terms of speed the Linq method will be the slowest as it will search against every record in the list. If you use Redgate's Reflector to inspect the code that executes for the other three methods, you find that at the end of the call tree that they all enumerate sequentially through the list. Their only saving grace is that once a match is found, they stop searching.

Based on the class compareto method, you are defining equality based solely on the extension field. Is each extension in the list unique? If so, then using a dictionary would give you better search performance.

Thank you for the informed reply TnTinMN. I am going to look into using a dictionary. There are NO duplicates. This file was created from a Registry app I found online to list all registered file extensions on the user’s computer I just tweaked it to get the other 2 fields. It's a labor of love learning vb.net by myself. Pretty much my fav hobby. I just started with the Lambda/LINQ portion. I did time the searches and there wasn't much difference as there are only ~1000 records. I may test it on my zip code database and see if there is a significant time difference or what I was mostly concerned with overhead.
Again Thank You for your time
KROD