question

Hobbyistprogrammer-7674 avatar image
0 Votes"
Hobbyistprogrammer-7674 asked karenpayneoregon answered

Relate two objects

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

dotnet-visual-basic
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.