我正在研究一种查询泛型类型并获取该类型列表的方法。但是,我希望能够向此添加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不会抱怨它们。
目前该方法到处都有红线
答案 0 :(得分:1)
如果您对约束使用正确的语法,那么看起来很好:
public static List<T> getList<T>() where T : someClass