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...
The Type T must be a reference type in order to use it as a parameter T.
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();
But was keen to use the features of the query api. Any ideas? maybe missing something obvious!!
You are missing a restriction on T
:
public IQueryable<T> FindAll<T>() where T : class
{
return Session.QueryOver<T>().List().AsQueryable();
}
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.
For a complete overview of generic type parameter constraints, see msdn.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With