组和总和列表(vb.net)

组和总和列表(vb.net),vb.net,linq,group-by,Vb.net,Linq,Group By,我有一张物品清单 Class Bundledtellingen Property tTimestamp As String Property tValue As Double End Class 现在我试着用tTimestamp和sum tValue对这个列表进行分组。通过搜索类似的问题,很明显最好的方法是使用Linq。我把这些放在一起: Dim query = bundledlist.GroupBy( _ Function(te

我有一张物品清单

    Class Bundledtellingen
        Property tTimestamp As String
        Property tValue As Double
    End Class
现在我试着用tTimestamp和sum tValue对这个列表进行分组。通过搜索类似的问题,很明显最好的方法是使用Linq。我把这些放在一起:

    Dim query = bundledlist.GroupBy( _
    Function(telling) telling.tTimestamp, _
    Function(telling) telling.tTimestamp, _
    Function(ts, values) New With _
        {.Key = ts, _
        .Sum = values.Sum()} _
    )

    Using writer As StreamWriter = New StreamWriter("C:\test.txt")
        For Each result In query
            writer.WriteLine(result.Key & " " & result.Sum)
        Next
    End Using

就我所知,通过查看下面的示例,这应该是可行的,不过我得到了以下例外:“重载解析失败,因为没有可访问的'Sum'接受这个数量的参数。”

您在
GroupBy
调用中指定了一个不必要的参数。你的那个:

Dim query = bundledlist.GroupBy( _
                Function(telling) telling.tTimestamp, _
                Function(ts, values) New With _
                    {.Key = ts, _
                    .Sum = values.Sum()} _
                )
第一个是键选择器,第二个是结果选择器


使用3个参数的调用具有附加值选择器,您可以在其中选择两次
函数(告诉)告诉.tTimestamp
,一次用于
,一次用于
。这就是为什么你的
Sum
不起作用:你试着在
telling.tTimestamp

上给
Sum
打电话。嗨,谢谢你给我指出了正确的方向。我只需要用telling.tValue替换第二个telling.tTimestamp。