Saurabh Kumar
Saurabh Kumar

Reputation: 2367

How to cast result back to generic list (using linq to object)?

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)})

I am not able to cast the above result back into the original object form from the below code:

    Dim myTable1_grouped As New EntityDATATable(Of EntityTableRow) 
    myTable1_grouped.TableRows = CType(lFinal2, List(Of EntityTableRow))

original class is like below. The class has a number of more string properties, which I have omitted for this snippet. All those properties also are using in the grouping in above linq code.:

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

The error I am getting is:

System.InvalidCastException was caught Message="Unable to cast object of type 'WhereSelectEnumerableIterator2[VB$AnonymousType_12[VB$AnonymousType_010[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_235[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]]]' to type 'System.Collections.Generic.List`1[Addin.EntityTableRow]'."

Upvotes: 2

Views: 4106

Answers (1)

MarcinJuraszek
MarcinJuraszek

Reputation: 125630

There are 2 things you have to do there:

Set your class name when creating the results:

Into Group Select New EntityTableRow With { _

instead of:

Into Group Select New With { _

And add ToList() to enumerate and get results into a List<EntityTableRow>:

myTable1_grouped.TableRows = CType(lFinal2.ToList(), List(Of EntityTableRow))

Upvotes: 1

Related Questions