以下查询与EF5配合良好,但给我带来了EF6错误。我使用DbContext和CodeFirst。无法将匿名类型从'System.Linq.IQueryable'转换为'System.Data.Entity.Core.Objects.ObjectQuery'
错误与EF6提出:
无法转换的类型“System.Linq.IQueryable
1[[<>f__AnonymousType5
4 [[System.Int32,mscorlib程序,版本= 4.0.0.0,文化=中性公钥= 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]]'键入'System.Data.Entity.Core.Objects .ObjectQuery1[[<>f__AnonymousType5
4 [[System.Int32,mscorlib,Version = 4.0.0.0,Culture = neutral,Publi cKeyToken = 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仅支持投射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();
这个问题似乎是在左连接与变量“订单”装匿名类型。我也尝试在'orders'查询中添加一个ToList(),但是错误更改为:
无法创建'匿名类型'类型的常量值。只有原始类型或枚举类型在此上下文中受支持。
有人能告诉我如何解决它吗?
更新:该错误已在EF 6.0.2中得到确认和解决。 See the Issue on CodePlex.
检查每个对象的'StorageLocationId'和'Level'的类型。机会是一个可以为空而另一个不可以,或者一个是长而另一个是整数或沿着这些行的东西。 – Servy
我现在不在PC前面,但我很确定所有值都是整数。此外,该例外显示了两种类型(IQuerable和ObjectQuery)每种都有四个int。在第一个查询(tos)中,我有一个可为空的int,但我使用了.Value。我明天早上再来检查一下。 Thx – dna2
尝试删除'UnloadingOrders = -1' – AaronLS