Relate two objects

Hobbyist_programmer 621 Reputation points
2021-07-13T14:54:55.817+00:00

Hallo,

for example I have two list. Let say if have same IDs in both lists how can i select automatically the object in the Details list according to the object selected in the Sample list from binding source. I know using LINQ i can get the values i want , is there any other way so i can refer to something like CType(ID, Details).Description ?

Public Class  Sample1

Public Property ID as Integer
Public Property Item as String

End Class

Public Class  Details

Public Property ID as Integer
Public Property Description as String

End Class

Thanks

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,567 questions
0 comments No comments
{count} votes

Accepted answer
  1. Karen Payne MVP 35,031 Reputation points
    2021-07-14T00:47:38.447+00:00

    Key here is join and group e.g. conceptual/working example done in a form for ease of coding.

    Public Class Form1
        Private Sub SampleButton_Click(sender As Object, e As EventArgs) Handles SampleButton.Click
    
            Dim sampleList = Mocked.Sample1List()
            Dim detailList = Mocked.DetailsList()
    
            Dim joinedResults = (
                    From sampleItem In sampleList 
                    Join detailItem In detailList On detailItem.Sample1Id Equals sampleItem.ID 
                    Select New Result With {
                        .Id = sampleItem.ID, 
                        .Item = sampleItem.Item, 
                        .Description = detailItem.Description}
            ).ToList()
    
            Dim grouped = joinedResults.GroupBy(Function(result) result.Id).ToList()
    
            For Each grouping As IGrouping(Of Integer, Result) In grouped
                Console.WriteLine(grouping.Key)
                For Each item As Result In grouping
                    Console.WriteLine($"    {item.Description}")
                Next
            Next
    
            Console.WriteLine()
    
            Console.WriteLine("Get id 2 items")
            Dim singleResult As IEnumerable(Of Result) = grouped.Select(Function(grouped) grouped).FirstOrDefault(Function(item) item.Key = 2)
            For Each result As Result In singleResult
                Console.WriteLine($"    {result.Description}")
            Next
        End Sub
    End Class
    
    Public Class Result
        Public Property Id As Integer
        Public Property Item As String
        Public Property Description As String
    End Class
    
    Public Class Sample1
        Public Property ID As Integer
        Public Property Item As String
    End Class
    
    Public Class Details
        Public Property ID As Integer
        Public Property Sample1Id As Integer
        Public Property Description As String
    End Class
    
    Public Class Mocked
        Public Shared Function Sample1List() As List(Of Sample1)
            Return New List(Of Sample1) From {
                New Sample1() With {.ID = 1, .Item = "A"},
                New Sample1() With {.ID = 2, .Item = "B"},
                New Sample1() With {.ID = 3, .Item = "C"}
            }
        End Function
        Public Shared Function DetailsList() As List(Of Details)
            Return New List(Of Details) From {
                New Details() With {.ID = 1, .Sample1Id = 1, .Description = "1st"},
                New Details() With {.ID = 2, .Sample1Id = 1, .Description = "2nd"},
                New Details() With {.ID = 3, .Sample1Id = 1, .Description = "3rd"},
                New Details() With {.ID = 4, .Sample1Id = 2, .Description = "111"},
                New Details() With {.ID = 5, .Sample1Id = 2, .Description = "222"},
                New Details() With {.ID = 6, .Sample1Id = 3, .Description = "111"}
            }
        End Function
    End Class
    

    Results

    1
        1st
        2nd
        3rd
    2
        111
        222
    3
        111
    
    Get id 2 items
        111
        222
    
    0 comments No comments

0 additional answers

Sort by: Most helpful