Search a List of Structure using Contains or Find method

Posted on 2011-10-11
Last Modified: 2012-06-27

I have a simple structure:

    Public Structure EmployeeStructure
        Public id As Integer
        Public name As String
    End Structure

I load the structure with a for loop
        Dim lStr As New EmployeeStructure
        Dim myList As New List(Of EmployeeStructure)
        For i = 1 To 10
   = i
   = "name" + i.ToString()

I want to check if myList has an employee with id=7.  This is a key, so I'm expecting to find it either once or not at all.   I don't want to loop thru the entire list.  I know I could use a simple LINQ query.  

Can I accomplish this using the contains, find, or findall method?  If so, please provide the code.  (Ideally for both and C#)

Question by:pttoy
    LVL 40

    Accepted Solution

    In order to use Contains, you need to know from the start which object you are looking for, so it does not apply to your situation.

    I order to use Find, you need to prepare a Predicate, a routine that tells the system how to find, that is the criteria that would match your serach value to an element in the list. That is more job for you, and what you will end up will be code that loops for you. You won't gain anything.

    The facts is that there is no way to find something without a loop. Not matter if you perform the loop yourself, use Find or FindAll, or use LINQ. The only difference is who is performing the loop, and I bet that your's would be slightly faster, because you would not have to go through an intermediate to do it for you.
    LVL 85

    Assisted Solution

    by:Mike Tomlinson's going to loop no matter how you look at it.  The "cleanest" code would be produced by LINQ.

    Here's one way to do it with the Find() function:
    Public Class Form1
        Public Structure EmployeeStructure
            Public id As Integer
            Public name As String
        End Structure
        Private idToFind As Integer
        Private myList As New List(Of EmployeeStructure)
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            For i As Integer = 1 To 10
                Dim lStr As New EmployeeStructure
       = i
       = "name" + i.ToString()
        End Sub
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            idToFind = 12
            Dim index As Integer = myList.IndexOf(myList.Find(Function(item As EmployeeStructure) = idToFind))
            If index <> -1 Then
                MessageBox.Show("Found at Index: " & index)
                MessageBox.Show("Not Found")
            End If
        End Sub
    End Class

    Open in new window


    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now