LINQ to Collection error, 'Unable to cast object of type'

Posts   
 
    
0010
User
Posts: 2
Joined: 24-Aug-2009
# Posted on: 15-Feb-2011 15:50:09   

Using LLBLGen 2.6, 28-Jan-2011 Release, 09-Oct-2009 build

Here is the code:

            Dim trans As New Transaction(IsolationLevel.ReadCommitted, "MyLinqTrans")
            Dim metaData As New LinqMetaData(trans)
            Dim q As ILLBLGenProQuery = DirectCast(( _
                    From c In metaData.Tickets _
                    Where c.ProjectID = 4 AndAlso c.RequestTypeID = 2 _
                    Group By c.ProjectName, EnteredYear = c.DateEntered.Year, EnteredMonth = c.DateEntered.Month _
                    Into NumTickets = Count() _
                    Select ProjectName, EnteredMonth, EnteredYear, NumTickets _
            ), ILLBLGenProQuery)

            Dim ticketRange As TicketsCollection = q.Execute(Of TicketsCollection)()
            ticketRange.GetMulti(Nothing)

And the error message:

Unable to cast object of type 'System.Collections.Generic.List`1[VB$AnonymousType_3`4[System.String,System.Int32,System.Int32,System.Int32]]' to type 'IME.data.CollectionClasses.TicketCollection'. 

I have looked at numerous forum posts with similar errors but nothing seems to point to my problem except maybe I should be using LLBLGen 3.1 because there are some bugs in 2.6? Maybe I read that wrong though. Either way, the project doesn't want to go to 3.1 unless we really have to. Can someone point me in the right direction with LINQ?

Alternatively, I don't even really HAVE to use LINQ for this. I just need to get a collection back with only certain fields, grouped that way and with the count. LINQ seemed like the easiest option, but now I don't know if that is the case. Any help is mucho appreciated.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39908
Joined: 17-Aug-2003
# Posted on: 15-Feb-2011 17:31:19   

The ILLBLGenProQuery cast you're doing won't work as that interface is for a query which fetches entities (so you get an EntityCollection(Of <type>) back.

You're fetching anonymous type instances. Also I don't understand what you're trying to do, as you first define a linq query, then execute it and then you do a getmulti confused

Frans Bouma | Lead developer LLBLGen Pro
0010
User
Posts: 2
Joined: 24-Aug-2009
# Posted on: 15-Feb-2011 19:46:04   

It would seem what I am doing is not getting enough sleep and then creating bad code that makes no sense! But thankfully everything you just said made sense and I got it to work. As you said, there is no reason to do an execute and then a getMulti. execute was already giving me what I needed, I just didn't know it! Thanks for the quick reply!

This works like a dream:

            Dim trans As New Transaction(IsolationLevel.ReadCommitted, "MyLinqTrans")
            Dim metaData As New LinqMetaData(trans)
            Dim q As ILLBLGenProQuery = DirectCast(( _
                    From c In metaData.UltraTicketCompletion _
                    Where c.ProjectID = 4 AndAlso c.RequestTypeID = 2 _
                    Group By c.ProjectName, EnteredYear = c.DateEntered.Year, EnteredMonth = c.DateEntered.Month _
                    Into NumTickets = Count() _
                    Select ProjectName, EnteredMonth, EnteredYear, NumTickets _
            ), ILLBLGenProQuery)

            myGrid.DataSource = q.Execute()
            myGrid.DataBind()
MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 15-Feb-2011 21:10:05   

What he said...!

Glad to know it's working now.

Matt