2
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="Unable to cast object of type 'WhereSelectEnumerableIterator 2[VB$AnonymousType_12[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.IEnumerable1[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.Dictionary2[System.Int32,System.Double]]]' 类型为 'System. Collections.Generic.List`1[Addin.EntityTableRow]'。”

4

1 回答 1

1

你必须在那里做两件事:

创建结果时设置你的类名:

Into Group Select New EntityTableRow With { _

代替:

Into Group Select New With { _

并添加ToList()枚举并将结果放入List<EntityTableRow>

myTable1_grouped.TableRows = CType(lFinal2.ToList(), List(Of EntityTableRow))
于 2013-03-19T19:48:10.947 回答