Unable to cast Anonymous Type from 'System.Linq.IQueryable' to 'System.Data.Entity.Core.Objects.ObjectQuery'

c# entity-framework entity-framework-6 linq linq-to-entities

Question

The following query was working well with EF5 but gives me error with EF6. I use DbContext and CodeFirst.

Error raised with EF6:

Unable to cast the type 'System.Linq.IQueryable1[[<>f__AnonymousType54[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], Wms.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=85d69d39f5becc93]]' to type 'System.Data.Entity.Core.Objects.ObjectQuery1[[<>f__AnonymousType54[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], Wms.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=85d69d39f5becc93]]'. LINQ to Entities only supports casting EDM primitive or enumeration types.

Query (simplified):

var unitsPerPosition = context.Positions.Include(p => p.Unit).Select(x => new { Unit = x.Unit, Position = x });

var orders = (
                from order in context.Orders.Include(x => x.SourceLocation);
                join unitPosition in unitsPerPosition on order.UnitId equals unitPosition.Unit.UnitId
                group new { order, unitPosition } by new { LocationId = order.SourceLocationId, Level = unitPosition.Position.Level } into groupOrders
                where groupOrders.Key.LocationId != null
                select new { 
                    LocationId = groupOrders.Key.LocationId.Value, 
                    Level = groupOrders.Key.Level, 
                    LoadingOrders = groupOrders.Count(), 
                    UnloadingOrders = -1 }
            );

IList<LocationChannels> searchResult = (from s in context.Locations
        .Include(s => s.Positions)
        .Include(s => s.Positions.Select(sp => sp.Unit))

        let channels = from p in s.Positions
                        where p.LocationId == s.LocationId
                        group p by new { p.LocationId, p.Column, p.Level } into channelsGroup

                        join order in orders on new { channelsGroup.Key.LocationId, channelsGroup.Key.Level } 
                        equals new { order.LocationId, order.Level } into ordersGroup
                        from o in ordersGroup.DefaultIfEmpty()

                        select new Channel
                        {
                            LocationId = channelsGroup.Key.LocationId,
                            Column = channelsGroup.Key.Column,
                            Level = channelsGroup.Key.Level
                        }

        select new LocationChannels
                      {
                        Location = s,
                        Channels = channels,
                      })
    .Where(predicate).ToList();

The issue appears to be on the left join with the anonymous type loaded in variable 'orders'. I also tried to add a ToList() to the 'orders' query but then the error change to:

Unable to create a constant value of type 'Anonymous type'. Only primitive types or enumeration types are supported in this context.

Can someone gives me an hint on how to fix it?

UPDATE: The bug has been confirmed and resolved in EF 6.0.2. See the Issue on CodePlex.

Popular Answer

As noted by dna2 at the end of his question, this issue was fixed in EF 6.0.2. To upgrade your version of EF, do the following in the Package Manager Console (VS2013 VIEW -> Other Windows -> Package Manager Console).

Update-Package EntityFramework -Version 6.0.2 



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why