创建IQueryable< T>在EF中使用基类进行扩展 [英] Create IQueryable<T> extension using base class in EF

查看:88
本文介绍了创建IQueryable< T>在EF中使用基类进行扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在基类上创建可重用的搜索查询,因此我不必为每个派生的类重复相同的代码,但是我无法使实体框架正常运行.

I am trying to create reusable search queries on my base class so i don't have to have the same code repeated for each dervived class but i cannot get entity framework to play nicely.

我有3个班级: CMEntityBase CMSite CMSiteServer

I have 3 classes: CMEntityBase CMSite CMSiteServer

网站和SiteServer均源自具有共同属性(ID,名称等)的CMEntityBase

Site & SiteServer are both derived from CMEntityBase which has common properties (ID, name, etc)

我想定义一些通用搜索: 例如GetByName

I want to define some generic searches: e.g. GetByName

using (var db = new LNOSCMDataModel())
            {
                db.Configuration.LazyLoadingEnabled = false;
                var servers = db.CMSiteServers.
                    AsNoTracking().
                    GetByName(id,Active).
                    ConvertToAPIVM().ToList();
}

我尝试了几种定义GetByName的方法:

i have tried defining GetByName several ways:

基类:

    public static IQueryable<CMEntityBase> GetByName(this IQueryable<CMEntityBase> Entities, string Name, bool Active = true)
    {
        return Entities.Where(ss => ss.Name == Name && ss.Active == Active || Active == false);//.Cast<IEntity>();
    }

泛型:

public static IQueryable<T> GetByName<T>(this IQueryable<CMEntityBase> Entities, string Name, bool Active = true) where T : CMEntityBase
        {
            return Entities.Where(ss => ss.Name == Name && ss.Active == Active || Active == false).Cast<T>();
        }

我尝试将基类定义为接口,并在泛型中使用T:class,IEntity(interface) ->此方法来自:

i've tried defining the base class as an interface and in the generic using T : class, IEntity (interface) --> this approach came from: LINQ to Entities only supports casting EDM primitive or enumeration types with IEntity interface

最终它们都返回错误:

LINQ to Entities仅支持使用IEntity接口转换EDM基本类型或枚举类型.

LINQ to Entities only supports casting EDM primitive or enumeration types with IEntity interface.

最后,我想在基类属性上定义查询,但输出子类.现在看来,我需要根据派生类复制/粘贴我的方法.

ultimately i want to define a query on the base class properties but output the child class. right now it appears i need to copy/paste my methods per derived class.

推荐答案

而不是接受与您想要的类型不同的IQueryable并尝试对其进行转换(不支持该错误,错误提示)您只需要接受您的查询已经是的实际类型的IQueryable,这样就无需进行强制转换.在这种情况下,就像在原始查询中使用泛型类型而不是基本类型一样简单:

Rather than accepting an IQueryable of a different type than what you want, and trying to cast it (which, as the error indicates, is not supported) you simply need to accept an IQueryable of the actual type that your query already is, so that there is no need to cast it. In this case it's as simple as using the generic type in the original query, rather than the base type:

public static IQueryable<T> GetByName<T>(this IQueryable<T> Entities, string Name, bool Active = true)
    where T : CMEntityBase //or the interface that specifies the needed members
{
    return Entities.Where(ss => ss.Name == Name && ss.Active == Active || Active == false);
}

这篇关于创建IQueryable&lt; T&gt;在EF中使用基类进行扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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