尝试做一些我确定应该很简单的事情,但在 linq 和 VB 中挣扎。我有以下内容,我正在按属性对集合进行分组,然后想要访问我的组的属性。我遇到的问题是匿名打字。由于选项严格,我必须给出一个明确的类型,但我无法确定该类型应该是什么。以下内容无法编译,因为 t.HeadAccountKey 不是值(因为 t 具有对象类型)。所以要么我需要做一些转换,要么我的 linq 选择器是错误的,
public class TempObject
public HeadKey as string
Public SubKey as string
Public SomeValue as decimal
end class
public class GroupTest
public sub RunTest
dim collection new List(of TempObject) = GetTestCollection()
Dim groupedValues As IEnumerable = From n In collection _
Group By key = n.HeadKey Into Group Select HeadKeyValue = key, SubValues = Group
for each t as object in groupedValues
debug.print(t.HeadKeyValue)
next
End sub
private function GetTestCollection() as List(of TempObject)
dim result as new list(of tempobject)
Dim x As New tempObject
x.HeadAccountNumber = "A"
x.SubAccountNumber = "B"
x.Value = 500
result.Add(x)
x = New tempObject
x.HeadAccountNumber = "A"
x.SubAccountNumber = "C"
x.Value = 600
result.Add(x)
x = New tempObject
x.HeadAccountNumber = "B"
x.SubAccountNumber = "D"
x.Value = 100
result.add(x)
return result
end function
End class