Abdul Ahmad
Abdul Ahmad

Reputation: 10021

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

Upvotes: 1

Views: 521

Answers (1)

Najera
Najera

Reputation: 2879

Looks fine if you use the correct syntax for the constraint:

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

Upvotes: 1

Related Questions