none
For each Loop Help RRS feed

  • Question

  • I'm trying to dig into this class and try to build a massive string to store the data. Trying to figure out the best way to split this data up. 

    class:

    Public Class Image
            Public Property page As Integer
            Public Property url As String
        End Class
    
        Public Class Item
            Public Property sku As String
            Public Property quantity As Integer
            Public Property substrate As String
            Public Property diecut_shape As String
            Public Property images As Image()
        End Class
    
        Public Class Order
            Public Property order_id As String
            Public Property shipping_first_name As String
            Public Property shipping_last_name As String
            Public Property shipping_address1 As String
            Public Property shipping_address2 As String
            Public Property shipping_city As String
            Public Property shipping_state As String
            Public Property shipping_zip As String
            Public Property shipping_country As String
            Public Property shipping_email As String
            Public Property shipping_phone As String
            Public Property shipping_method As String
            Public Property items As Item()
        End Class


    I need to split the item and then I need to split the image. 

    Ive tried the following:

             

    For Each item As _Order.Item In _Order
        For Each img As item.image In item

        Next
    Next

    but I get syntax errors. So just need a example on how to dig down into this object. 

    Thursday, October 10, 2019 4:54 PM

Answers

  • See if this might help (not to be perfect in regards to nulls) The output is a simple concatenation with no delimiter.

    Imports System.Text
    
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim sb As New StringBuilder
    
            MockedOrders.Select(Function(item) item.items.images.ToList()).
                ToList().ForEach(
                    Sub(item)
                        For Each image As Image In item
                            sb.Append(image.url)
                        Next
                    End Sub)
    
    
            Console.WriteLine(sb.ToString())
    
        End Sub
        Private Function MockedOrders() As List(Of Order)
            Dim orderList As New List(Of Order)
    
            orderList.Add(New Order With {.order_id = "1", .items = New Item() With
                             {.images = New Image() {New Image With {.page = 1, .url = "AA1"},
                                                     New Image With {.page = 2, .url = "AA2"},
                                                     New Image With {.page = 3, .url = "AA3"}}}})
            orderList.Add(New Order With {.order_id = "2", .items = New Item() With
                             {.images = New Image() {New Image With {.page = 22, .url = "BB1"},
                                                     New Image With {.page = 23, .url = "BB2"},
                                                     New Image With {.page = 3, .url = "BB3"}}}})
    
            Return orderList
        End Function
    
    End Class


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, October 10, 2019 9:43 PM
    Moderator

All replies

  • Hi,
    try this:

          Dim Orderlist As New List(Of Order)
    
          ' Fill OrderList and Items and Images
    
          For Each orderItem In Orderlist
            If orderItem.items IsNot Nothing Then
              For Each itemItem In orderItem.items
                If itemItem.images IsNot Nothing Then
                  For Each itemImage In itemItem.images
    
                  Next
                End If
              Next
            End If
          Next


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Thursday, October 10, 2019 5:08 PM
  • I think this is right? Does this look right?

    Private Function BuildItemString(ByVal orders As Domain.Models.Order.Order) As String
                Dim lst As List(Of Domain.Models.Order.Item) = orders.items.ToList()
    
                For Each item As Domain.Models.Order.Item In lst
                    For Each img As Domain.Models.Order.Image In item.images
    
                    Next
                Next
            End Function

    no syntax errors

    • Marked as answer by old_School Tuesday, October 15, 2019 3:21 PM
    • Unmarked as answer by old_School Tuesday, October 15, 2019 3:21 PM
    Thursday, October 10, 2019 6:17 PM
  • See if this might help (not to be perfect in regards to nulls) The output is a simple concatenation with no delimiter.

    Imports System.Text
    
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim sb As New StringBuilder
    
            MockedOrders.Select(Function(item) item.items.images.ToList()).
                ToList().ForEach(
                    Sub(item)
                        For Each image As Image In item
                            sb.Append(image.url)
                        Next
                    End Sub)
    
    
            Console.WriteLine(sb.ToString())
    
        End Sub
        Private Function MockedOrders() As List(Of Order)
            Dim orderList As New List(Of Order)
    
            orderList.Add(New Order With {.order_id = "1", .items = New Item() With
                             {.images = New Image() {New Image With {.page = 1, .url = "AA1"},
                                                     New Image With {.page = 2, .url = "AA2"},
                                                     New Image With {.page = 3, .url = "AA3"}}}})
            orderList.Add(New Order With {.order_id = "2", .items = New Item() With
                             {.images = New Image() {New Image With {.page = 22, .url = "BB1"},
                                                     New Image With {.page = 23, .url = "BB2"},
                                                     New Image With {.page = 3, .url = "BB3"}}}})
    
            Return orderList
        End Function
    
    End Class


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, October 10, 2019 9:43 PM
    Moderator
  • Hi,

    Have you solved this problem now?

    I think the above reply can provide you with a solution, have you tried it?

    If so, hope you can close this thread by marking the reply as answer as this will help others looking for the same or similar issues down the road.

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, October 14, 2019 7:48 AM
    Moderator