【问题标题】:How to order inner keysource in linq vb group join如何在 linq vb 组加入中订购内部密钥源
【发布时间】:2016-01-12 00:44:23
【问题描述】:

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

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

我在Ordering inner keysource in simple/unnamed C# LINQ group join 找到了与 C# 类似的东西 但还没有找到在 VB 中执行此操作的方法 - 感谢任何想法。

【问题讨论】:

标签: vb.net linq join


【解决方案1】:

您在提问时需要提供以下信息:

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

当这段代码运行时,它会产生:

为确保内部数据的顺序,您需要像这样更改查询:

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

现在它产生了:

【讨论】:

  • 正是我所需要的——我也感谢有关发布的反馈
  • @MikeDiggins - 如果它对您有用,请不要忘记投票并选择作为接受的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多