哪里是演员吗? LINQ到实体仅支持铸造实体数据模型的基本类型 [英] Where is the cast here? LINQ to Entities only supports casting Entity Data Model primitive types

查看:88
本文介绍了哪里是演员吗? LINQ到实体仅支持铸造实体数据模型的基本类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我公司拥有一批实体框架的表,我已经做出支持使用他们的parital类 IHistoricEntity 的接口。 IHistoricEntity ActiveTo 日期时间?属性。

I have a number of entity framework tables that I have made support an interface IHistoricEntity using their parital classes. IHistoricEntity has ActiveTo Datetime? property.

// Auto generated LINQ to Entities domain service:
[EnableClientAccess()]
public partial class ProductService : LinqToEntitiesDomainService<ProductDBEntities>
{
    public IQueryable<ANALYSIS_CODES> GetANALYSIS_CODES()
    {
        return this.ObjectContext.ANALYSIS_CODES;
    }
 ...
}

// My Partial class to add interface
public partial class ANALYSIS_CODES : IHistoricEntity
{}

我试图重构这个工作代码的方法:

I am trying to refactor this working code to a method:

List<ANALYSIS_CODE> filtered = (from rec in ps.GetANALYSIS_CODES() where rec.ActiveTo == null select rec).ToList()

像这样:

private List<T> Filter<T>(IQueryable<T> queryable) where T : IHistoricEntity
{
    return (from rec in queryable where rec.ActiveTo == null select rec).ToList();
}

// called like this:
List<ANALYSIS_CODE> filtered = Filter(ps.GetANALYSIS_CODES());

这给出了该异常的了ToList

无法转换类型'ANALYSIS_CODES为键入'IHistoricEntity。 LINQ到实体仅支持铸造实体数据模型的基本类型。

Unable to cast the type 'ANALYSIS_CODES' to type 'IHistoricEntity'. LINQ to Entities only supports casting Entity Data Model primitive types.

但是,在我已要求它强制转换为 IHistoricEntity ?我已经mearly说, T 必须支持 IHistoricEntity

But where have I asked it to cast to an IHistoricEntity? I have mearly said that T must support IHistoricEntity.

推荐答案

rec.ActiveTo 指的是在你的接口中定义的属性。因此LINQ的需要能够访问该属性之前投录制 IHistoricEntity

rec.ActiveTo refers to a property defined in your interface. Therefore Linq needs to cast rec to IHistoricEntity before being able to access that property.

不要被例外愚弄的 .ToList被提出():LINQ查询仅被评估,并在需要记录时执行,在这种情况下,当收集要转变成一个列表与LT;>

Don't be fooled by the exception being raised in .ToList(): the Linq query is only evaluated and executed when the records are needed, in this case, when the collection is to be transformed into a List<>.

更新:我验证@ HVD的评论,确实增加了其中T:类条款的改变,从

Update: I verified @hvd's comment, and indeed, adding a where T: class clause changes the Linq expression from

System.Collections.Generic.List`1[MyType]
    .Where(x => (Convert(x).ActiveTo == Convert(null)))

System.Collections.Generic.List`1[MyType]
    .Where(x => (x.ActiveTo == Convert(null)))

这篇关于哪里是演员吗? LINQ到实体仅支持铸造实体数据模型的基本类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆