是否有可能做创建一个通用Session.QueryOver< T> ;?
出于好奇是否有可能做这样的事情使用NHibernate 3
Out of curiosity is it possible to do something like this using NHibernate 3?
public IQueryable<T> FindAll<T>()
{
return Session.QueryOver<T>().List().AsQueryable();
}
我得到一个编译错误说像...
I get a compilation error saying something like...
类型T必须是为了使用它作为一个参数T引用类型。
The Type T must be a reference type in order to use it as a parameter T.
我在想,如果我可以创造扩展方法来Session.QueryOver来处理泛型类型。
I was wondering if I could create an extension method to Session.QueryOver to handle a generic type.
我可以取代这个使用类似
I could replace this using something like
return Session.CreateCriteria(typeof (T)).List<T>().AsQueryable();
不过,热衷于使用查询API的功能。有任何想法吗?也许缺少明显的东西!
But was keen to use the features of the query api. Any ideas? maybe missing something obvious!!
您缺少对 T的限制
:
public IQueryable<T> FindAll<T>() where T : class
{
return Session.QueryOver<T>().List().AsQueryable();
}
,其中T:类
定义 T
必须是引用类型。 (由于编译错误要求,apperently QueryOver< T>
被限制为引用类型)。如果一个类型参数已经应用到它的限制,使用该方法具有自身的一个通用的参数的任何一般方法具有施加类似的限制。
where T : class
defines that T
has to be a reference type. (As the compile error demanded, apperently QueryOver<T>
is restricted to reference type). If a type parameter has constraints applied to it, any generic method using this method with a generic parameter of its own has to apply similar constraints.
有关的通用完整概述类型参数的限制,请参见 MSDN 。
For a complete overview of generic type parameter constraints, see msdn.