Linq query is failing

Anthony Matovu
Anthony Matovu used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Retired
Distinguished Expert 2017
Commented:
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial