我需要遍历购物车,按代码和日期分组,然后对匹配相同代码和日期的整数求和。不确定LINQ是否会对此有所帮助。如果它更容易避免LINQ那么棒。
这是我的班级:
Class CartProduct
Property ActCode As String
Property Customers As Integer
Property Date As Date
End Class
这是我的功能:
Function GetTotals() As IEnumerable(Of String)
Dim totalList As New List(Of String)
For Each p As CartProduct In _products
totalList.Add(p.ActCode)
totalList.Add(p.Customers)
totalList.Add(p.Date)
Next
Dim result = From newList In totalList
Group by totalList.ActCode and totalList.Date
Sum totalList.Customers ???
Select newList {}
Return result
End Function
以下是_products返回的内容:
Friend Property Products As List(Of CartProduct)
Get
Return _products
End Get
Set(value As List(Of CartProduct))
_products = value
End Set
End Property
我无法访问CartProduct属性以在新列表中执行该组。这就是我绊倒的原因。
编辑:希望这会引导我和其他人走向富有成效的方向。 How to sum similar element of an array of structure in vb.net?
感谢。