我已经浏览了几个小时的互联网,没有找到任何可以帮助我的情况,我认为这是很常见的情况。
我想在列表框中计算特定项目,并找出每个项目的数量。我想将这个值赋给变量。我想我已经完成了大部分工作,但是我无法让代码工作。
要解释我想要的东西在哪里计算我在列表中有多少'蒜味面包',然后将其分配给变量,如'item1',依此类推所有项目。
干杯
答案 0 :(得分:0)
Public Class MyListItem
Public Sub New(id As Integer, name As String)
LiId = id
LiName = name
End Sub
Public LiId As Integer
Public LiName As String
End Class
Private Sub Test()
Dim listbox2 As New ListBox()
Dim list As New List(Of MyListItem)()
list.Add(New MyListItem(1, "a"))
list.Add(New MyListItem(2, "a"))
list.Add(New MyListItem(3, "b"))
list.Add(New MyListItem(4, "c"))
listbox2.DataSource = list
listbox2.ValueMember = "LiId"
listbox2.DisplayMember = "LiName"
Dim countOfA = Aggregate itm In DirectCast(listbox2.DataSource, List(Of MyListItem))
Where itm.LiName = "a"
Into Sum(1)
End Sub
结果:countOfA = 2