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.IQueryable
1[[<>f__AnonymousType5
4[[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__AnonymousType5
4[[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.
StorageLocationId
andLevel
from each of those object. Chances are one is nullable and another isn't, or one is a long and another an int or something along those lines. – ServyUnloadingOrders = -1
– AaronLS