After posting a [question](http://stackoverflow.com/questions/19455105/ef5-query-doesnt-work-with-ef6) at StackOverflow about my issue, some user suggest me to post the error here.
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.ObjectQuery`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]]'. 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?
Comments: @ajcvickers - The change looks pretty simple. We have been hit by this, and rather than take a nightly build that has who-knows-what in it, I'm inclined to cherry-pick your commit on top of 6.0.1 and build it myself. Any reason you can think of this would be a bad idea?
Thanks