我有这个查询,按预期工作:
Dim test = result.GroupBy(Function(row) groupedindexes.Select(
Function(grpindex) row(grpindex)).ToArray, comp).
Select(Function(g) New With {g.Key, Key .sumOff4 = g.Sum(Function(x) Convert.ToInt32(x(3)))})
g.Key
是这样的:a,b,c,.sumOff4
是一个简单的数字。
现在我希望IEnumerable(Of Object())
或List(Of Object())
有这样的输出:a,b,c,10。
我尝试过ToList,ToArray等,但没有得到正确的结果。
基本上,我想在gridview中显示数据,其中g.Key.Count
可能会有所不同,如下所示:
dgv.Columns.Add("f1", "f1")
dgv.Columns.Add("f2", "f2")
dgv.Columns.Add("f3", "f3")
dgv.Columns.Add("f4", "f4")
For Each item In test
dgv.Rows.Add(item)
Next
但我在一栏中得到了关键和总和:
我想在4个coluns中拥有值。我怎么能这样做?
编辑:
与此同时,我明白了。我创建了一个帮助类:
Partial Class groupedresult
Private _keys As Object()
Private _aggr_values As New List(Of Object)
Public Sub New(ByVal keys As Object(), ByVal ParamArray aggr_values() As Object)
_keys = keys
For i = 0 To aggr_values.Count - 1
_aggr_values.Add(aggr_values(i))
Next
End Sub
Public Property keys As Object()
Get
Return _keys
End Get
Set(ByVal value As Object())
_keys = value
End Set
End Property
Public Property aggregated_values As List(Of Object)
Get
Return _aggr_values
End Get
Set(ByVal value As List(Of Object))
_aggr_values = value
End Set
End Property
End Class
并更改了这样的查询:
Dim test = result.GroupBy(Function(row) groupedindexes.Select(
Function(grpindex) row(grpindex)).ToArray, comp).
Select(Function(g) New groupedresult(g.Key, g.Sum(Function(x) Convert.ToInt32(x(3)))))
然后我编码了这个:
Dim objects As New List(Of Object)
Dim lst As New List(Of Object())
For Each item In test
objects.AddRange(item.keys)
objects.AddRange(item.aggregated_values)
lst.Add(objects.ToArray)
objects.Clear()
Next
然后我可以写:
For Each item In lst
dgv.Rows.Add(item)
Next
答案 0 :(得分:0)
我不确定这正是您正在寻找的, 但它可能有助于指导您解决方案。
据我了解, 你有LINQ查询,在一个集合上工作正常, 并且该对象的集合并不像您期望的那样工作。
投影集合时, 您可以将SelectMany用于您描述的用途。
代码示例:
Dim strings = New List(Of String) From {"A", "B", "E", "E", "A", "B", "E"}
Dim groupedStrings = strings.
GroupBy(Function(o) o).
Select(Function(o) New With {.Key = o.Key, .Count = o.Count}).
ToList()
Dim stringsCollection = New List(Of List(Of String)) From {
New List(Of String) From {"A", "B", "E", "E", "A", "B", "E"},
New List(Of String) From {"B", "B", "A", "A", "A", "B", "E"},
New List(Of String) From {"A", "A", "E", "E", "E", "E", "E"}
}
Dim groupedStringsFromCollection = stringsCollection.
SelectMany(Function(o) o).
GroupBy(Function(o) o).
Select(Function(o) New With {.Key = o.Key, .Count = o.Count}).
ToList()
希望这有帮助。