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 发布于 2015-06-08 23:10:13
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发布于 2015-06-08 23:11:50
您的'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 beeGrouphttps://stackoverflow.com/questions/30711870
复制相似问题