NH Translate ICriteria to QueryOver
I would like to transform this piece of code to be used with QueryOver
public IList<T> ListByCriteria( ICriteria criteria, int maxResult )
{
IList<T> ret = new List<T>();
using (ITransaction tx = m_session.BeginTransaction(IsolationLevel.ReadCommitted))
{
try
{
ret = criteria
.SetMaxResults( maxResult )
开发者_运维知识库 .List<T>();
tx.Commit();
}
finally
{
if (tx.IsActive)
tx.Rollback();
}
}
return ret;
}
to something like
public IList<T> ListByQueryOver( Expression<Func<bool>> expression )
{
IList<T> ret = new List<T>();
using (ITransaction tx = m_session.BeginTransaction(IsolationLevel.ReadCommitted))
{
try
{
ret = m_session.QueryOver<T>().Where( expression )
.List<T>();
tx.Commit();
}
finally
{
if (tx.IsActive)
tx.Rollback();
}
}
return ret;
}
but it doesn't compile. The error message states: "T must be a reference type in order to use it as parameter 'T'" on QueryOver
Isn't it possible to make this call generic?
What's wrong?
Thank you, Stefano
Try:
public IList<T> ListByQueryOver( Expression<Func<T, bool>> expression ) where T : class, new()
The where clause restricts T to reference types, and new requires a parameter-less constructor on the class. The QueryOver Where method requires an `Expression>' parameter.
精彩评论