IOrderedEnumerable to vb.net IOrderedEnumerable Преобразование

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    

2 ответа

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

Ваш оператор "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

Или пользовательский параметр Infer On:

Dim beeGroups = From bee In Me.world.Bees
                Group bee By bee.CurrentState Into beeGroup = Group
                Order By CurrentState
                Select beeGroup
Другие вопросы по тегам