Grouping Objects to New Object Class

Viewed 58

I have an object class that contains information about anchors and down guy wires and create a list of those objects.

Public Class AnchorsAndDownGuysInformation

Public Sub New(ByVal aCoordX As String,
               ByVal aCoordA As String,
               ByVal dDesc As String,
               ByVal dCoordZ As String,
               ByVal dDiameter As String,
               ByVal dRtsStrength As String,
               ByVal dPoundsPerInch As String)

    CoordXAnchor = aCoordX
    CoordAAnchor = aCoordA
    Description = dDesc
    CoordZDown = dCoordZ
    Diameter = dDiameter
    RTSStrength = dRtsStrength
    PoundsPerInch = dPoundsPerInch

End Sub

Public Property CoordXAnchor As String
Public Property CoordAAnchor As String
Public Property Description As String
Public Property CoordZDown As String
Public Property Diameter As String
Public Property RTSStrength As String
Public Property PoundsPerInch As String

End Class

For example sake, lets say there are four items in the list of AnchorsAndDownGuysInformation. Two of them have the same CoordXAnchor and CoordAAnchor. I need to group them by those values and send them to another class Anchors which references the Downguys class.

Public Class Anchors

Public Property CoordXAnchor As String
Public Property CoordAAnchor As String
Public Property DownGuyInfo As New List(Of DownGuys)

End Class

Public Class DownGuys

Public Property Description As String
Public Property CoordZDown As String
Public Property Diameter As String
Public Property RTSStrength As String
Public Property PoundsPerInch As String

End Class

So the final result would be a list of Anchor Class; three (3) anchors, one (1) with two (2) DownGuys and two (2) anchors with one (1) DownGuy each.

I have tried a few things but cant seem to get it to work.

 Dim groupedAnchors = anchorsAndDownGuyList.GroupBy(Function(x) New With {x.CoordAAnchor, x.CoordXAnchor
                        }).[Select](Function(anc) New Anchors() With {
                        .CoordAAnchor = anc.Key.CoordAAnchor,
                        .CoordXAnchor = anc.Key.CoordXAnchor,
                        .DownGuyInfo = anc.[Select](Function(dg) New DownGuys() With {
                        .Description = dg.Description,
                        .CoordZDown = dg.CoordZDown,
                        .Diameter = dg.Diameter,
                        .RTSStrength = dg.RTSStrength,
                        .PoundsPerInch = dg.PoundsPerInch
                        })})

I cant seem to get this to work...

I have also tried just grouping them and then trying to send them to the class after the grouping. A two step approach. I can get them grouped but cant seem to access the data afterwards to then send to the class.

What I have tried....

    Dim groupedAnchors = anchorsAndDownGuyList.GroupBy(Function(j) New With {Key j.CoordAAnchor, Key j.CoordXAnchor}).
                         Select(Function(group) group.Key.CoordAAnchor).ToList()

    Dim groupedAnchors2 = anchorsAndDownGuyList.GroupBy(Function(a) New With {Key a.CoordAAnchor, Key a.CoordXAnchor}).ToList()

I hope I explained myself well and someone can help me out. Thank you in advance.

1 Answers

Not Sure if this is the best way, but I found a way to achieve what I was trying to do. I took the two step approach. I figured I would post it in case anyone else was looking to do the same.

    Private Sub CombineAnchorsAndDownGuys(ByVal anchorsAndDownGuyList As List(Of FieldExtractAnchorsAndDownGuysInformation))

    Dim combinedAnchors As New List(Of Anchors)

    Dim groupedAnchorDownGuys = From anchorDownGuy In anchorsAndDownGuyList
                                Group anchorDownGuy By key = New With {Key anchorDownGuy.CoordA, anchorDownGuy.CoordX}
                                Into anchorDownGuyGroup = Group

    For Each anchor In groupedAnchorDownGuys

        Dim combinedDownGuys As New List(Of DownGuys)

        For Each guyWire In anchor.anchorDownGuyGroup

            combinedDownGuys.Add(New DownGuys(guyWire.Description, guyWire.CoordZ, guyWire.Diameter, guyWire.RTSStrength, guyWire.PoundsPerInch))

        Next

        combinedAnchors.Add(New Anchors(anchor.key.CoordX, anchor.key.CoordA, combinedDownGuys))

    Next

End Sub
Related