c# - 无法将类型 'DynamicClass1' 转换为类型 <T>。 LINQ to Entities 仅支持转换 EDM 原语或枚举类型

标签 c# .net linq entity-framework entity-framework-6

我尝试了几个小时来搜索我的问题的解决方案,但没有任何帖子可以帮助我。

我正在尝试使用带库的 Linq( Entity Framework 6.0/MVC 5.0)从数据库中获取记录。

System.Linq.Dynamic;

完整代码如下

using (var entities = new vskdbEntities())
{
     entity.DataFields = "id, stack_trace";
     _list = entities.vsk_error_log
           .OrderBy(entity.Order)
           .Select("New(" + entity.DataFields + ")")
           .Skip(entity.PageSize * entity.PageNumber)
           .Take(entity.PageSize)
           .Cast<vsk_error_log>()
           .ToList();
}

但是在运行时这会导致错误

An exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll but was not handled in user code

Additional information: Unable to cast the type 'DynamicClass1' to type 'VideoKit.framework.vsk_error_log'. LINQ to Entities only supports casting EDM primitive or enumeration types.

我知道 Select 子句存在转换问题,但不知道如何解决此转换问题。

更新一:

如果我尝试使用如下所示的代码,它将正确获取数据

using (var entities = new vskdbEntities())
{
     var context = ((System.Data.Entity.Infrastructure.IObjectContextAdapter)entities).ObjectContext;

     lst = context.CreateObjectSet<vsk_error_log>().AsEnumerable().Select("new (id, stack_trace)").Cast<DynamicClass>().ToList();
 }

但是检索到的数据类型是

Generic List of System.Linq.Dynamic.DynamicClass

代替 vsk_error_log

如果我尝试使用正确的类 vsk_error_log 而不是 DynamicClass 进行转换,则会出现错误

Unable to cast object of type 'DynamicClass1' to type 'VideoKit.framework.vsk_error_log'.

返回的数据如截图所示

enter image description here

最佳答案

要从动态查询中获取实体本身,您必须使用 it 关键字,而不是 new:

using (var entities = new vskdbEntities())
{
     _list = entities.vsk_error_log
           .OrderBy(entity.Order)
           .Select("it") // here "it"
           .Skip(entity.PageSize * entity.PageNumber)
           .Take(entity.PageSize)
           .ToList();
}

根据您的评论,我了解到您正在尝试获取部分填充的 vsk_error_log 实体。您可以使用查询的主要部分执行此操作,然后将 DynamicClass 实例转换为实体:

var result = new List<vsk_error_log>();

using (var entities = new vskdbEntities())
{
    _list = entities.vsk_error_log
           .OrderBy(entity.Order)
           .Skip(entity.PageSize * entity.PageNumber)
           .Take(entity.PageSize)
           .Select("new (id, stack_trace)");

    foreach(dynamic d in _list)
    {
        result.Add(new vsk_error_log { id = d.id, stack_trace = d.stack_trace } );
    }
}

更新: 完整的代码,可以很好地满足下面的期望输出

var _list = new List<vsk_error_log>();
using (var entities = new vskdbEntities())
{
    var context = ((IObjectContextAdapter)entities).ObjectContext;
    var lst = context.CreateObjectSet<vsk_error_log>()
         .AsEnumerable()
         .Select("new(id,stack_trace)")
         .Cast<DynamicClass>()
         .Skip(entity.PageSize * (entity.PageNumber - 1))
         .Take(entity.PageSize)
         .ToList();
         foreach (dynamic d in lst)
         {
             _list.Add(new vsk_error_log { id = d.id, stack_trace = d.stack_trace });
         }
}

关于c# - 无法将类型 'DynamicClass1' 转换为类型 <T>。 LINQ to Entities 仅支持转换 EDM 原语或枚举类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26430375/

相关文章:

c# - 取消异步操作

c# - 读取和写入文件的最简单方法

c# - TransactionScope 超时过早发生?

c# - 如何使用linq计算运行总计

c# - 如何将 LINQ 嵌套 Selectmany 转换为 SQL 正则语句

c# - 为什么 C# 编译器在将方法组作为参数传递时不解析仅返回类型不同的委托(delegate)类型?

c# - 显示 "Save as"窗口

c# - 查找二叉树中叶到根路径的最大总和

.Net WCF/Web服务异常处理设计模式

c# - LINQ: ...Where(x => x.Contains(以 "foo"开头的字符串 ))