Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB.NET 2010 search an array using .find instead of looping thru all values to find

Status
Not open for further replies.

DougP

MIS
Dec 13, 1999
5,985
US
I inherited this code and it gets a list of employees and adds them to the array masterEmployeeList.
But as it loads, it also is checking to see if it found someone already. it sdoes this by looping thru the entire list it is building every time to find them. is there a way to use something like >>>
Array.FindAll(masterEmployeeList, New ArrayComparer(thisEmployee.strEid).Contains)
so it can instantly find them or not
if so how?

Code:
       Dim thisEmployee As Employee = New Employee
        thisEmployee.strEid = thisResult.Properties("vzeid")(0).ToString()
              [highlight #FCE94F] For Each existingEmployee As Employee In masterEmployeeList
            If existingEmployee.strEid = thisEmployee.strEid Then
                Exit Sub
            End If
        Next[/highlight]        
        thisEmployee.strVzid = thisResult.Properties("vzid")(0).ToString()
        thisEmployee.strName = thisResult.Properties("cn")(0).ToString()
....

TIA

DougP
 

This assumes there is no default strEID values set for new instances of Employee, otherwise you'll have to recode the comparison logic. I prefer to pass the whole object to the function, but that's just me...

Code:
Function EmployeeExists(ByVal EmployeeToSearchFor As Employee) As Boolean
        Dim emp As New Employee
        emp = Array.Find(masterEmployeeList, Function(e As Employee) e.strEId = EmployeeToSearchFor.strEId)
        Return (emp.strEId IsNot Nothing)
    End Function

and called like so:

Code:
blnEmployeeExists = EmployeeExists(thisEmployee)




Mark

"You guys pair up in groups of three, then line up in a circle."
- Bill Peterson, a Florida State football coach
 
MarkSweetland this is the same error I was getting

Data type(s) of the type parameter(s) in method 'Public Shared Function Find(Of T)(array() As T, match As System.Predicate(Of T)) As T' cannot be inferred from these arguments. Specifying the data type(s) explicitly might correct this error.

on the line as follows
emp = Array.Find(masterEmployeeList, Function(e As Employee) e.strEId = EmployeeToSearchFor.strEId)



DougP
 
This is kind of a roundabout way to do this, but give it a try:

Create a datatable and add rows to it in parallel with the array. Then you can use the RowFilter method of the datatable's dataview to see if a particular item has already been added.

Dim dcEID As DataColumn
Dim dtEIDs As DataTable
Dim dv As DataView
Dim dr As DataRow

dcEID = New DataColumn()
dtEIDs = New DataTable

dtEIDs.Columns.Add(dcEIDs)

dv = dtEIDs.DefaultView

'When it's time to check if an employee is already in the array:


dv.RowFilter = "EID='" & thisResult.Properties("vzeid")(0).ToString() & "'"

If dv.Count = 0 Then 'this indicates the employee is not in the datatable already, so add to the datatable and array
dr = dtEIDs.NewRow
dr.item("EID") = thisResult.Properties("vzeid")(0).ToString()
dtEIDs.Rows.Add(dr)
dr = Nothing
'***Set up employee object here, and add to array
End If



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 

Another method using LINQ:

Code:
Dim empExistingEmployee = From emp In masterEmployeeList
    Where (emp.strEId = thisEmployee.strEId)

Dim blnThisEmployeeExists As Boolean = (empExistingEmployee.Count > 0)



Mark

"You guys pair up in groups of three, then line up in a circle."
- Bill Peterson, a Florida State football coach
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top