• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 234
  • Last Modified:

Search a List of Structure using Contains or Find method


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
            lStr.id = i
            lStr.name = "name" + i.ToString()
            myList.Add(lStr)
        Next


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 VB.net and C#)

Thanks,
0
pttoy
Asked:
pttoy
2 Solutions
 
Jacques Bourgeois (James Burger)PresidentCommented:
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.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Agreed...it'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
            lStr.id = i
            lStr.name = "name" + i.ToString()
            myList.Add(lStr)
        Next
    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) item.id = idToFind))
        If index <> -1 Then
            MessageBox.Show("Found at Index: " & index)
        Else
            MessageBox.Show("Not Found")
        End If
    End Sub

End Class

Open in new window

0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now