【问题标题】:IOrderedEnumerable to vb.net IOrderedEnumerable ConversionIOrderedEnumerable 到 vb.net IOrderedEnumerable 的转换
【发布时间】:2015-06-08 14:28:09
【问题描述】:
Private Sub UpdateListBox(message As String)
    Dim beeGroups As IOrderedEnumerable(Of IGrouping(Of BeeState, Bee)) = From beeGroup In From bee In Me.world.BeesGroup bee By bee.CurrentStateOrder By beeGroup.KeybeeGroup
    Me.listBox1.Items.Clear()

    For Each beeGroup As IGrouping(Of BeeState, Bee) In beeGroups
        Dim s As String
        s = If(beeGroup.Count() = 1, String.Empty, "s")

        Me.listBox1.Items.Add(Convert.ToString(beeGroup.Key) & ": " & beeGroup.Count() & " bee" & s)

        If beeGroup.Key <> BeeState.Idle OrElse beeGroup.Count() <> Me.world.Bees.Count() OrElse Me.framesRun <= 0 Then
            Continue For
        End If

        Me.listBox1.Items.Add("Simulation ended: all bees are idle")
        Me.toolStripButtonStartSimulation.Text = "Simulation ended."
        Me.statusStrip1.Items(0).Text = "Simulation ended"
        Me.timer1.Enabled = False
    Next
End Sub    

在将c# 项目转换为vb.net 时,我完全陷入了这个错误。

我知道c#是这样的

IOrderedEnumerable<IGrouping<BeeState, Bee>> beeGroups = from bee in this.world.Bees
                                                                 group bee by bee.CurrentState
                                                                 into beeGroup orderby beeGroup.Key select beeGroup;    

但一旦转换为vb.net,它会尝试用最少的声明在一行上声明所有内容。

c# 中,您也可以逃脱 c#

foreach (IGrouping<BeeState, Bee> beeGroup in beeGroups)    

vb.net 尝试在自身内部声明 for 语句:

For Each group As var In beeGroups    

【问题讨论】:

  • I'm absolutely stuck at this error in the conversion of a c# project to vb.net 究竟是什么错误?

标签: c# asp.net .net vb.net iorderedenumerable


【解决方案1】:
Private Sub UpdateListBox(message As String)
    Dim beeGroups = From bee In Me.world.Bees
                    Group By bee.CurrentState Into beeGroup = Group
                    Order By beeGroup.Key

    Me.listBox1.Items.Clear()

    For Each beeGroup In beeGroups
        Dim beeCount = beeGroup.Count ' to avoid reevaluation
        Dim s = If(beeCount >= 1, String.Empty, "s")

        Me.listBox1.Items.Add(Convert.ToString(beeGroup.Key) & ": " & beeCount & " bee" & s)

        If beeGroup.Key <> BeeState.Idle OrElse beeCount <> Me.world.Bees.Count OrElse Me.framesRun <= 0 Then
            Continue For
        End If

        Me.listBox1.Items.Add("Simulation ended: all bees are idle")
        Me.toolStripButtonStartSimulation.Text = "Simulation ended."
        Me.statusStrip1.Items(0).Text = "Simulation ended"
        Me.timer1.Enabled = False
    Next
End Sub

【讨论】:

    【解决方案2】:

    您的“For Each”语句是正确的(您的 C# 和 VB 版本都在循环头中声明了迭代器变量),但是 VB LINQ 查询应该是:

    Dim beeGroups As IOrderedEnumerable(Of IGrouping(Of BeeState, Bee)) = From bee In Me.world.Bees
                                                                          Group bee By bee.CurrentState Into beeGroup = Group
                                                                          Order By CurrentState
                                                                          Select beeGroup
    

    或用户选项推断开启:

    Dim beeGroups = From bee In Me.world.Bees
                    Group bee By bee.CurrentState Into beeGroup = Group
                    Order By CurrentState
                    Select beeGroup
    

    【讨论】:

    • Select 默认情况下在 VB.Net 中不需要,查询返回当前在查询范围内的内容(在这种情况下只有 beeGroup)。此外,您忘记在Order By 子句中通过bee. 限定CurrentState ;)
    • select beeGroup from Dim beeGroups 之后。当我事后声明 For Each beeGroup In beeGroup 时,它的行为就像 select beeGroup 甚至不存在。我可以看到 beeGroup 在 beeGroup 中声明,错误是混淆。我会以错误的方式使用 IEnumerable 并声明吗?谢谢你之前的帮助:)
    • 好的,错误是它没有被声明,我的困惑太大了,无法放入变量中:D。但是他们是在beeGroups之前声明beeGroup的一种方式,或者如果你能想到一个更好的方式。
    猜你喜欢
    • 1970-01-01
    • 2011-03-05
    • 1970-01-01
    • 1970-01-01
    • 2012-03-06
    • 2012-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多