List of List Of

frutuopa

Member
Joined
Apr 4, 2022
Messages
10
Programming Experience
10+
Hello

Imagine that I have a class of people and each of these people has some friends.
I can't find the correct syntax to join a person with several friends.

Can you help?

Thanks


VB.NET:
Public Class Amigos
    Public Property nome As String
    Public Property idade As Integer

    Public Sub New(ByVal _nome As String, ByVal _idade As Integer)
        Me.nome = _nome
        Me.idade = _idade
    End Sub

End Class

Public Class pessoa
    Public Property id As Integer
    Public Property nome As String
    Public Property idade As Integer
    Public Property sexo As Nullable(Of Char)

    Public Property amigos As List(Of Amigos)

    Public Sub New(ByVal _id As Integer, ByVal _nome As String, ByVal _idade As Integer, ByVal _sexo As Char, ByVal _amigos As List(Of Amigos))
        Me.id = _id
        Me.nome = _nome
        Me.idade = _idade
        Me.sexo = _sexo
        Me.amigos = _amigos
    End Sub


    Public Shared Function getpessoas() As List(Of pessoa)
        Dim listapessoas As New List(Of pessoa) From
            {
                New pessoa(1, "Paulo", 45, "M", (New Amigos("Joao", 10), New Amigos("Antonio", 10))
            }
        Return listapessoas
    End Function

 
End Class
 
Last edited by a moderator:
Firstly, when a class has property that is a collection type, it is almost always the right thing to do to make that property read-only. That means that you can get the collection in order to get its items or modify it but you cannot replace the entire collection or set the property to null. If you then want to accept items via a constructor, you would use a parameter of type IEnumerable(Of T) and populate the existing collection internally. That means that you can pass any list that implements that interface. E.g.
VB.NET:
Public Class Parent

    Public Property Name As String
    Public ReadOnly Property Children As New List(Of Child)
   
    Public Sub New(name As String, children As IEnumerable(Of Child))
        Me.Name = name
        Me.Children.AddRange(children)
    End Sub
   
End Class

Public Class Child

    Public Property Name As String
   
    Public Sub New(name As String)
   
End Class
VB.NET:
Dim parents As New List(Of Parent) From
    {
        New Parent("Parent 1",
                   {
                       New Child("Child 1"),
                       New Child("Child 2")
                   })
    }
In this case, I'm using a literal array of Child objects as an argument to the Parent constructor but any list will do.

If there's some genuine reason you can't do it like this, let me know and we'll revisit the alternatives. It's rare that not doing it this way is a good idea though, although it's not unheard of.
 
Another Method, it should get you started as an example piece:
How to join on multiple lists only by list index:
Try something like below:

data:

Dim aList As New ArrayList({"a", "b", "c"})
Dim bList As New ArrayList({"1", "2", "3"})
Dim cList As New ArrayList({"#", "*", "!"})
class:

Public Class StringTuple
    Public a_Type As String
    Public b_Type As String
    Public c_Type As String
End Class
And the Query is

    Dim output1 = From n1 In aList
                  Join n2 In bList On aList.IndexOf(n1) Equals bList.IndexOf(n2)
                  Join n3 In cList On bList.IndexOf(n2) Equals cList.IndexOf(n3)
                  Select New StringTuple With {.a_Type = n1, .b_Type = n2, .c_Type = n3}
 
Back
Top