0
Public Class B1
    Public G2 As New List(Of Person)
End Class
Public Class Person
    Public Property PerId As Integer
    Public Property FirstName As String
    Public Property LastName As String
End Class
Public Class All4One
    Public Property ID As Integer
    Public Property Dept As String

    Public Overrides Function ToString() As String
        Dim _pers As New Person
        _pers = G2.Item(??)
        Return _pers.FirstName & " " & _pers.LastName
    End Function
End Class

Here is where I am hung up. I want to find the index(= to ??) in G2 where _pers.PerId = ID.

Thank you for your assistance. GregV

T.S.
  • 18,195
  • 11
  • 58
  • 78

1 Answers1

3

This is what you need to do - use LINQ extensions. You don't have to, because you can manually iterate your list if you want. But I don't see what you're going to use for searching it? And also you use ToString() completely wrong - Only class person should have a Tostring containing Return pers.FirstName & " " & pers.LastName. Technically it is possible as below:

Public Class All4One
    Private _perId as integer 'I added it to use for search as an example

    Public Property ID As Integer
    Public Property Dept As String

    'WARNING!!!! BAD DESIGN
    Public Overrides Function ToString() As String
        Dim pers As Person = G2.FirstOrDefault(function(p) p.PerId = _perId)
        If pers isNot Nothing Then            
           Return pers.FirstName & " " & pers.LastName
        Else
           'return whatever you want
           Return "No Person found"
        End If
    End Function
End Class

You need to add something to search person by. Also, if you have a contract that only one unique PersID can be found in the list, you can use SingleOrDefault instead of FirstOrDefault to shave some milliseconds.

T.S.
  • 18,195
  • 11
  • 58
  • 78
  • Use FirstOrDefault() if you want to call it on something other than a List. Use FirstOrDefault() if you might want to call it on something other than a L ist in the future. The .Find method will stop when it finds a match based on the predicate passed. LINQ on the other hand will go through the whole list looking for a match and possibly return many items. On that note, they both use the same logic to traverse the list. – Trevor Jul 18 '14 at 02:27
  • 1
    @MrCoDeXeR Disagree http://stackoverflow.com/questions/9335015/find-vs-where-firstordefault – T.S. Jul 18 '14 at 02:32
  • I would use SingleOrDefault because your saying you want only one, the one you used can return many... – Trevor Jul 18 '14 at 02:33
  • Check Here.. http://stackoverflow.com/questions/1745691/linq-when-to-use-singleordefault-vs-firstordefault-with-filtering-criteria – Trevor Jul 18 '14 at 02:36
  • @MrCoDeXeR You're right in a way. But we really don't know if there will be duplicates. I believe, `SingleOrDefault` will error out if many found. I updated the answer – T.S. Jul 18 '14 at 02:43
  • Correct, he could have many or one... good update and solution. – Trevor Jul 18 '14 at 02:53