Dim a As New List(Of EntityTableRow)
a = myTable1.TableRows
Dim lFinal2 = (From el In a Group el By Key = New With {Key el.Description, Key el.Activity, Key el.ServiceGroup, Key el.SubServiceGroup, Key el.ResourceGroup, Key el.Country, Key el.DCN, Key el.Workforce, Key el.RateType, Key el.LoadType} _
Into Group Select New With { _
.PageNum = "", _
.Description = Key.Description, _
.Activity = Key.Activity, _
.MonthCosts = (From k In Group.SelectMany(Function(g) g.MonthCosts.Keys).Distinct() _
Select New With {.Month = k, .Sum = Group.Sum(Function(g) _
If(g.MonthCosts.ContainsKey(k), g.MonthCosts(k), 0))}).ToDictionary(Function(i) i.Month, Function(i) i.Sum)})
我无法将上述结果从以下代码转换回原始对象形式:
Dim myTable1_grouped As New EntityDATATable(Of EntityTableRow)
myTable1_grouped.TableRows = CType(lFinal2, List(Of EntityTableRow))
原始课程如下。该类有许多字符串属性,我已为此代码段省略了这些属性。所有这些属性也在上面的linq代码中使用分组。:
Public Class EntityTableRow
Public PageNum As Integer
Public Description As String
Public Activity As String
.....
.....
.....
Public MonthCosts As New Dictionary(Of Integer, Double)
End Class
我得到的错误是:
发现了System.InvalidCastException
Message =“无法转换类型的对象'WhereSelectEnumerableIterator 2[VB$AnonymousType_1
2 [VB $ AnonymousType_0 10[System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.String],System.Collections.Generic.IEnumerable
1 [Addin.EntityTableRow]],VB $ AnonymousType_2 35[System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.String,System.Collections.Generic.Dictionary
2 [System.Int32,System。 Double]]]'键入'System.Collections.Generic.List`1 [Addin.EntityTableRow]'。“
答案 0 :(得分:1)
你必须做两件事:
创建结果时设置您的班级名称:
Into Group Select New EntityTableRow With { _
而不是:
Into Group Select New With { _
添加ToList()
枚举并将结果导入List<EntityTableRow>
:
myTable1_grouped.TableRows = CType(lFinal2.ToList(), List(Of EntityTableRow))