扩展类的NHibernate queryover泛型类型

时间:2015-07-02 17:44:52

标签: c# generics nhibernate

我正在研究一种查询泛型类型并获取该类型列表的方法。但是,我希望能够向此添加Where(),因此我希望查询能够查询任何扩展或实现某个类/接口的类型。

    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;
    }

我喜欢这样的事情:

    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;
    }

someClass及其子项将包含IsDisabled, IsDeleted字段,以便queryOver不会抱怨它们。

目前该方法到处都有红线

1 个答案:

答案 0 :(得分:1)

如果您对约束使用正确的语法,那么看起来很好:

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