【问题标题】:Unable to cast Anonymous Type from 'System.Linq.IQueryable' to 'System.Data.Entity.Core.Objects.ObjectQuery'无法将匿名类型从“System.Linq.IQueryable”转换为“System.Data.Entity.Core.Objects.ObjectQuery”
【发布时间】:2013-10-27 14:50:43
【问题描述】:

以下查询在 EF5 上运行良好,但在 EF6 上出现错误。我使用 DbContext 和 CodeFirst。

EF6 引发的错误:

无法转换类型 '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,文化=中性,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,Culture=neutral,PublicKeyToken=85d69d39f5becc93]]' 键入'System.Data.Entity.Core.Objects.ObjectQuery1[[<>f__AnonymousType54[[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, 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,公众号cKeyToken=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 中得到确认和解决。 See the Issue on CodePlex.

【问题讨论】:

  • 检查每个对象的StorageLocationIdLevel 的类型。有可能一个是可以为空的,另一个不是,或者一个是 long 而另一个是 int 或类似的东西。
  • 我现在不在电脑前,但我很确定所有值都是整数。此外,异常显示两种类型(IQuerable 和 ObjectQuery)各有四个 int。在第一个查询(tos)中,我有一个可为空的 int,但我使用了 .Value。我明天早上先再检查一遍。谢谢
  • 尝试删除UnloadingOrders = -1
  • 您介意在entityframework.codeplex.com 上提交错误吗?如果可能,请提供堆栈跟踪和模型。这似乎是 EF6 中的一个错误。
  • 在此处发布:entityframework.codeplex.com/workitem/1751 我将尽快使用堆栈跟踪和模型进行编辑。

标签: c# linq entity-framework linq-to-entities entity-framework-6


【解决方案1】:

正如 dna2 在他的问题末尾所指出的,此问题已在 EF 6.0.2 中修复。要升级您的 EF 版本,请在包管理器控制台 (VS2013 VIEW -&gt; Other Windows -&gt; Package Manager Console) 中执行以下操作。

Update-Package EntityFramework -Version 6.0.2 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-21
    • 1970-01-01
    • 1970-01-01
    • 2016-02-07
    • 2018-10-29
    相关资源
    最近更新 更多