简体   繁体   中英

NHibernate queryover generic type that extends a class

I'm working on a method that queries over a generic type and gets a list of that type. However, I want to be able to add Where() to this, so I want the query over to be able to query over any type that extends or implements a certain class/interface.

    public static List<T> getList<T>() where T : class
    {
        List<T> clientList = null;
        using (ISession session = 
                 NHibernateSessionFactoryManager.Factory.OpenSession())
        {
            clientList = new List<T>(session.QueryOver<T>()
                //.Where(x => !x.IsDisabled) - won't work
                //.Where(x => !x.IsDeleted) - won't work
                .List());
        }
        return clientList;
    }

I'd like something like this:

    public static List<T:someClass> getList<T:someClass>() where T:someClass
    {
        List<T> clientList = null;
        using (ISession session = 
                 NHibernateSessionFactoryManager.Factory.OpenSession())
        {
            clientList = new List<T>(session.QueryOver<T>()
                .Where(x => !x.IsDisabled)
                .Where(x => !x.IsDeleted)
                .List());
        }
        return clientList;
    }

and someClass and its children would have the IsDisabled, IsDeleted fields so that queryOver doesn't complain about them.

currently the method has red lines everywhere

如果对约束使用正确的语法,则看起来不错:

public static List<T> getList<T>() where T : someClass

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM