Linq query is failing

I have the code below and i am getting the error message
Unable to cast object of type 'VB$AnonymousDelegate_0`2[System.Object,System.Int32]' to type 'System.IConvertible'.Couldn't store <VB$AnonymousDelegate_0`

at line     workRow("package_key") = q.package_key, i really dont know how to go about this. I am just leaning LINQ

aNTHONY



 Dim tmpquery = From obj_perhour In tmp_perhour.AsEnumerable() _
               Group obj_perhour By customerid = obj_perhour.Field(Of Decimal)("msisdn_nsk"), _
               time_key = obj_perhour.Field(Of Decimal)("time_key"), _
               base_station_key = obj_perhour.Field(Of Decimal)("base_station_key"), _
               faf_ind = obj_perhour.Field(Of String)("faf_ind") _
                Into grp = Group _
                  Select New With _
                       { _
                         .msisdn = customerid, .time_key = time_key, .base_station_key = base_station_key, .faf = faf_ind,
                         .package_key = Function(x) If((x.Field(Of Decimal)("package_key") = 21437 Or _
                                                        x.Field(Of Decimal)("package_key") = 22102 Or _
                                                        x.Field(Of Decimal)("package_key") = 21448 Or _
                                                        x.Field(Of Decimal)("package_key") = 18998 Or _
                                                        x.Field(Of Decimal)("package_key") = 21395), 1, _
                                                    If((x.Field(Of Decimal)("package_key") = 21437 Or _
                                                        x.Field(Of Decimal)("package_key") = 21353 Or _
                                                         x.Field(Of Decimal)("package_key") = 21361), 2, _
                                                    If((x.Field(Of Decimal)("package_key") = 612362 Or _
                                                        x.Field(Of Decimal)("package_key") = 359292 Or _
                                                        x.Field(Of Decimal)("package_key") = 605772 Or _
                                                        x.Field(Of Decimal)("package_key") = 1078419 Or _
                                                        x.Field(Of Decimal)("package_key") = 692109), 3, 4))),
                         .ccost = grp.Sum(Function(x) x.Field(Of Decimal)("ccost")),
                         .cdur = grp.Sum(Function(x) x.Field(Of Decimal)("cdur")),
                         .calls = grp.Count(Function(x) x.Field(Of Decimal)("msisdn_nsk"))
                         }

        createactivitylog("Created per hour table", startt, Now)
        startt = Now

        Dim dt As New DataTable("perhour")

        dt.Columns.Add("msisdn", GetType(Decimal))
        dt.Columns.Add("time_key", GetType(Decimal))
        dt.Columns.Add("base_station_key", GetType(Decimal))
        dt.Columns.Add("faf", GetType(String))
        dt.Columns.Add("package_key", GetType(Decimal))
        dt.Columns.Add("ccost", GetType(Decimal))
        dt.Columns.Add("cdur", GetType(Decimal))
        dt.Columns.Add("calls", GetType(Decimal))

        i = 0
        For Each q In tmpquery
            Dim workRow As DataRow = dt.NewRow()
            workRow("msisdn") = q.msisdn
            workRow("time_key") = q.time_key
            workRow("base_station_key") = q.base_station_key
            workRow("faf") = q.faf
            workRow("package_key") = q.package_key
            workRow("ccost") = q.ccost
            workRow("cdur") = q.cdur
            workRow("calls") = q.calls
            dt.Rows.Add(workRow)
            i += 1
              If i Mod 10000 = 0 Then Application.DoEvents()
        Next
LVL 1
Anthony MatovuBusiness Analyst, MTN UgandaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi  MatovuAnthony;

What I think is happening is that the query is assuming Integer values when you have a number such as 21437. Append a D after each number in the following code so that 21437 becomes 21437D so that the system knows you are using Decimal and NOT Integer's.

 .package_key = Function(x) If((x.Field(Of Decimal)("package_key") = 21437 Or _
                                                        x.Field(Of Decimal)("package_key") = 22102 Or _
                                                        x.Field(Of Decimal)("package_key") = 21448 Or _
                                                        x.Field(Of Decimal)("package_key") = 18998 Or _
                                                        x.Field(Of Decimal)("package_key") = 21395), 1, _
                                                    If((x.Field(Of Decimal)("package_key") = 21437 Or _
                                                        x.Field(Of Decimal)("package_key") = 21353 Or _
                                                         x.Field(Of Decimal)("package_key") = 21361), 2, _
                                                    If((x.Field(Of Decimal)("package_key") = 612362 Or _
                                                        x.Field(Of Decimal)("package_key") = 359292 Or _
                                                        x.Field(Of Decimal)("package_key") = 605772 Or _
                                                        x.Field(Of Decimal)("package_key") = 1078419 Or _
                                                        x.Field(Of Decimal)("package_key") = 692109), 3, 4))),

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.