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="タイプ'WhereSelectEnumerableIterator2 2[VB$AnonymousType_1
[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]]]'のオブジェクトをタイプ'システムにキャストできません。 Collections.Generic.List`1[Addin.EntityTableRow]'。 "