Projecting into anonymous class

Posts   
 
    
liggett78
User
Posts: 2
Joined: 01-Oct-2008
# Posted on: 01-Oct-2008 00:30:42   

Hi, I'm currently evaluating LLBL Gen Pro and have stumbled upon the following issue:

I'm doing this LINQ query:

from m in metaData.Messages
select new { Message = m }

which throws an exception:

Unhandled Exception: System.InvalidCastException: Unable to cast object of type
'System.Int32' to type 'XXX.EntityClasses.MessageEntity'.
   bei lambda_method(ExecutionScope , Object[] , Int32[] )
   bei SD.LLBLGen.Pro.LinqSupportClasses.DataProjectorToObjectList`1.AddRowToRes
ults(IList projectors, Object[] rawProjectionResult)
   bei SD.LLBLGen.Pro.LinqSupportClasses.DataProjectorToObjectList`1.SD.LLBLGen.
Pro.ORMSupportClasses.IGeneralDataProjector.AddProjectionResultToContainer(List`
1 valueProjectors, Object[] rawProjectionResult)
   bei SD.LLBLGen.Pro.ORMSupportClasses.ProjectionUtils.FetchProjectionFromReade
r(List`1 valueProjectors, IGeneralDataProjector projector, IDataReader datasourc
e, Int32 maxNumberOfItemsToReturn, Int32 pageNumber, Int32 pageSize, Boolean cli
entSideLimitation, Boolean clientSideDistinctFiltering, Boolean clientSidePaging
, UniqueList`1 stringCache, Dictionary`2 typeConvertersToRun)
   bei SD.LLBLGen.Pro.ORMSupportClasses.DaoBase.GetAsProjection(List`1 valueProj
ectors, IGeneralDataProjector projector, ITransaction transactionToUse, IRetriev
alQuery queryToExecute, Dictionary`2 typeConvertersToRun)
   bei SD.LLBLGen.Pro.ORMSupportClasses.DaoBase.GetAsProjection(List`1 valueProj
ectors, IGeneralDataProjector projector, ITransaction transactionToUse, IEntityF
ields fields, IPredicateExpression filter, IRelationCollection relations, Int32
maxNumberOfItemsToReturn, ISortExpression sortClauses, IGroupByCollection groupB
yClause, Boolean allowDuplicates, Int32 pageNumber, Int32 pageSize)
   bei SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProProvider.ExecuteValueListProj
ection(QueryExpression toExecute)
   bei SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProProviderBase.ExecuteExpressio
n(Expression handledExpression)
   bei SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProProviderBase.Execute(Expressi
on expression)
   bei SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProProviderBase.System.Linq.IQue
ryProvider.Execute(Expression expression)
   bei SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProQuery`1.Execute()
   bei SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProQuery`1.System.Collections.Ge
neric.IEnumerable<T>.GetEnumerator()
   bei System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   bei System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   bei ConsoleApplication1.Program.Main(String[] args) in C:\...

When I do

from m in metaData.Messages
select new { Message = m.MessageId }

though, everything is fine. Is there something I'm missing or is the first query not supported?

The reason for doing this is that I would like to return a wrapper class that would contain a message plus the number of replies for that message, e.g.

from m in metaData.Messages
select new { Message = m, Count = (from c in m.Comments select c).Count() }

The Message-table is self-referencing with a nullable ParentMessageId. BTW, I'm using SQL Server 2005.

Thanks.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 01-Oct-2008 07:59:25   

Hi there, you can do that this way:

var q = from m in metaData.Messages
            select new 
            { 
                  Message = new { m.MessageId, m.Description, m.Sender }, 
                  Count = m.Comments.Count() 
            }
David Elizondo | LLBLGen Support Team
liggett78
User
Posts: 2
Joined: 01-Oct-2008
# Posted on: 01-Oct-2008 08:39:43   

daelmo wrote:

Hi there, you can do that this way:

var q = from m in metaData.Messages
            select new 
            { 
                  Message = new { m.MessageId, m.Description, m.Sender }, 
                  Count = m.Comments.Count() 
            }

Well, my MessageEntity contains quite a lot of information plus prefetched user/message category/attachments, so I would rather not have to list those 10-15 fields/objects in the new class.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39619
Joined: 17-Aug-2003
# Posted on: 01-Oct-2008 10:17:17   

This isn't supported. The problem with your original query is that 'm' isn't materialized into an Entity object and then stored in an anonymous type: the projection is doing a projection of the data coming from the db into an anonymous type. If you want this to happen, you first have to fetch the entities and then do a linq to objects query on the set of entities fetched to project them into anonymous types.

Frans Bouma | Lead developer LLBLGen Pro