Vb.net 将对象分组到新对象类

Vb.net 将对象分组到新对象类,vb.net,class,group-by,Vb.net,Class,Group By,我有一个对象类,它包含关于锚和下拉线的信息,并创建这些对象的列表 Public Class AnchorsAndDownGuysInformation Public Sub New(ByVal aCoordX As String, ByVal aCoordA As String, ByVal dDesc As String, ByVal dCoordZ As String,

我有一个对象类,它包含关于锚和下拉线的信息,并创建这些对象的列表

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
例如,假设主持人和主持人信息列表中有四项。其中两个具有相同的CoordXAnchor和CoordAAnchor。我需要根据这些值对它们进行分组,并将它们发送到另一个引用Downguys类的类锚

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
因此,最终结果将是锚类列表;三(3)个锚,一(1)个锚带两(2)个引下人,两(2)个锚带一(1)个引下人

我试过几件事,但似乎没能成功

 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
                        })})
我好像没法让它工作

我也尝试过将它们分组,然后在分组后将它们发送给班级。分两步走的方法。我可以让他们分组,但似乎不能访问数据后,然后发送到类

我所尝试的

    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()

我希望我能解释清楚,有人能帮我。提前谢谢。

我不确定这是否是最好的方法,但我找到了一种方法来实现我想做的事情。我采取了两步走的方法。我想我会把它贴出来,以防其他人也这么做

    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

谢谢你这么做。你的帖子写得很好,这篇后续文章表明你明白了这个想法。如果你仍然在寻找替代方案,我可能会看看这个,但不管怎样,我很高兴看到你对这个社区的贡献。欢迎光临@兰斯洛特非常感谢你的好话和提议。我认为我现在很好,因为它正在发挥作用,而且是始终如一的。:)如果他们回来想要改变,我可能会接受你。