如何在linq vb group join中订购内部键源

时间:2016-01-12 00:44:24

标签: vb.net linq join

我正在尝试使用组连接和VB.NET将表连接到自身。下面的代码工作并对外(父)行进行排序,但我想保证内(子)行的顺序:

{{1}}

我在Ordering inner keysource in simple/unnamed C# LINQ group join找到了与C#相似的内容 但是还没有找到在VB中做到这一点的方法 - 感谢任何想法。

1 个答案:

答案 0 :(得分:0)

以下是您在提问时需要提供的内容:

Public Class edata

    Public Ethnicity As Ethnicity() = _
    { _
        New Ethnicity() With { .EthnicityID = 4, .ParentId = 1, .RoundID = 3, .Sort = 8 }, _
        New Ethnicity() With { .EthnicityID = 3, .ParentId = 1, .RoundID = 4, .Sort = 5 }, _
        New Ethnicity() With { .EthnicityID = 1, .RoundID = 2 } _
    }

End Class

Public Class Ethnicity

    Public ParentID As Integer
    Public EthnicityID As Integer
    Public RoundID As Integer
    Public Sort As Integer

End Class

Sub Main

    Dim edata = New edata()
    Dim aRoundID = 2

    Dim queryEthnicities = _
        From aParentEthnicity In edata.Ethnicity _
        Group Join aChildEthnicity In edata.Ethnicity On aChildEthnicity.ParentID Equals aParentEthnicity.EthnicityID Into EthnicityList = Group _
        Where aParentEthnicity.RoundID.Equals(aRoundID) _
        Order By aParentEthnicity.Sort

End Sub

当此代码运行时,它会生成:

original

要确保内部数据的顺序,您需要更改查询,如下所示:

Dim queryEthnicities = _
    From aParentEthnicity In edata.Ethnicity _
    Group Join aChildEthnicity In edata.Ethnicity.OrderBy(Function (x) x.Sort) On aChildEthnicity.ParentID Equals aParentEthnicity.EthnicityID Into EthnicityList = Group _
    Where aParentEthnicity.RoundID.Equals(aRoundID) _
    Order By aParentEthnicity.Sort

现在它产生:

ordered