以下查询在 EF5 上运行良好,但在 EF6 上出现错误。我使用 DbContext 和 CodeFirst。
EF6 引发的错误:
无法转换类型 '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=中性,PublicKeyToken=b77a5c561934e089],[System.Int32,mscorlib,版本=4.0.0.0,文化=中性,PublicKeyToken=b77a5c561934e089],[System.Int32,mscorlib,版本=4.0.0.0,文化=中性,PublicKeyToken=b77a5c561934e089] ], Wms.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=85d69d39f5becc93]]' 输入'System.Data.Entity.Core.Objects.ObjectQuery1[[<>f__AnonymousType5
4[[System.Int32,mscorlib,版本=4.0.0.0,文化=中性,PublicKeyToken=b77a5c561934e089],[System.Int32,mscorlib,版本=4.0.0.0,文化=中性,PublicKeyToken=b77a5c561934e089],[System.Int32 ,mscorlib,版本=4.0.0.0,文化=中性,PublicKeyToken=b77a5c561934e089],[System.Int32,mscorlib,版本=4.0.0.0,文化=中性,PublicKeyToken=b77a5c561934e089]],Wms.Domain,版本=1.0.0.0 ,文化=中性,PublicKeyToken=85d69d39f5becc93]]'。LINQ to Entities 仅支持转换 EDM 基元或枚举类型。
查询(简化):
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();
问题似乎出在左侧加入变量“订单”中加载的匿名类型。我还尝试将 ToList() 添加到“订单”查询中,但随后错误变为:
无法创建“匿名类型”类型的常量值。此上下文仅支持原始类型或枚举类型。
有人可以给我一个关于如何解决它的提示吗?
更新:该错误已在 EF 6.0.2 中得到确认和解决。请参阅 CodePlex 上的问题。