LINQ unable to cast in projection query

Posts   
 
    
pat
User
Posts: 215
Joined: 02-Mar-2006
# Posted on: 02-Feb-2009 23:21:29   

Hi,

this works:

var q = (from c in metaData.FileTransferLog
         select c);
var list = new List<IO.Dailies.DailiesFileVersionDTO>();
foreach (var item in q)
{
    list.Add(CreateDailiesFileVersionDTO(item));
}

This throws the exception below when enumerating over the collection:

var t = (from c in metaData.FileTransferLog
         select CreateDailiesFileVersionDTO(c));

Any ideas why this might happen?

Thanks, Patrick

System.InvalidCastException was unhandled
  Message="Unable to cast object of type 'System.Int32' to type 'CFX.Amanda.DAL.EntityClasses.MyFileTransferLogEntity'."
  Source="Anonymously Hosted DynamicMethods Assembly"
  StackTrace:
       at lambda_method(ExecutionScope , Object[] , Int32[] )
       at SD.LLBLGen.Pro.LinqSupportClasses.DataProjectorToObjectList`1.AddRowToResults(IList projectors, Object[] rawProjectionResult)
       at SD.LLBLGen.Pro.LinqSupportClasses.DataProjectorToObjectList`1.SD.LLBLGen.Pro. ORMSupportClasses.IGeneralDataProjector.AddProjectionResultToContainer(List`1 valueProjectors, Object[] rawProjectionResult)
       at SD.LLBLGen.Pro.ORMSupportClasses.ProjectionUtils.FetchProjectionFromReader(List`1 valueProjectors, IGeneralDataProjector projector, IDataReader datasource, Int32 maxNumberOfItemsToReturn, Int32 pageNumber, Int32 pageSize, Boolean clientSideLimitation, Boolean clientSideDistinctFiltering, Boolean clientSidePaging, UniqueList`1 stringCache, Dictionary`2 typeConvertersToRun)
       at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.FetchProjection(List`1 valueProjectors, IGeneralDataProjector projector, IRetrievalQuery queryToExecute, Dictionary`2 typeConvertersToRun)
       at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.FetchProjection(List`1 valueProjectors, IGeneralDataProjector projector, IEntityFields2 fields, IRelationPredicateBucket filter, Int32 maxNumberOfItemsToReturn, ISortExpression sortClauses, IGroupByCollection groupByClause, Boolean allowDuplicates, Int32 pageNumber, Int32 pageSize)
       at SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProProvider2.ExecuteValueListProjection(QueryExpression toExecute)
       at SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProProviderBase.ExecuteExpression(Expression handledExpression)
       at SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProProviderBase.Execute(Expression expression)
       at SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProProviderBase.System.Linq.IQueryProvider.Execute[TResult](Expression expression)
       at System.Linq.Queryable.ElementAt[TSource](IQueryable`1 source, Int32 index)
  InnerException: 
Walaa avatar
Walaa
Support Team
Posts: 14951
Joined: 21-Aug-2005
# Posted on: 03-Feb-2009 10:12:19   

I don't think the second approach would work, as "c" is not of MyFileTransferLogEntity type.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39619
Joined: 17-Aug-2003
# Posted on: 03-Feb-2009 10:17:08   

The reason is that 'c' isn't materialized into an entity when it is passed into the method call at runtime: entities are fetched using the entity fetch pipeline and not the projection pipeline to be able to use polymorphism during fetching.

You could create a different CTor in the DTO and pass all fields separately, that would work too.

Frans Bouma | Lead developer LLBLGen Pro
pat
User
Posts: 215
Joined: 02-Mar-2006
# Posted on: 03-Feb-2009 18:53:40   

Otis wrote:

You could create a different CTor in the DTO and pass all fields separately, that would work too.

Makes sense.

Thanks you, Patrick

joshrivers
User
Posts: 10
Joined: 30-Dec-2008
# Posted on: 22-Feb-2009 03:04:08   

If it's possible, I'd like a bit more explanation on this issue. I'm pretty sure I've been bitten by it multiple times in the last week on my project, but I've been chasing it around in weird directions.

Is this a bug in the Linq provider? Is this a bug in Linq?

The syntax seems valid, but it doesn't work.

            var results = from a in metadata.MyEntity
                        where a.Id == 1 
                        select new MyDTO(a);
            return results .Single();

But this works:

            var results = from a in metadata.MyEntity
                        where a.Id == 1 
                        select a;
            return new MyDTO(results .Single());

I'd like to understand it if it's a 'Train the Monkey' (me) issue. If it's not, is it possible to fix?

Thanks!

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005