I write same query with two approach by using NHibernate:
1- by using HQL
like below
public long RetrieveHQLCount<T>(string propertyName, object propertyValue)
{
using (ISession session = m_SessionFactory.OpenSession())
{
long r = Convert.ToInt64(session.CreateQuery("select count(o) from " + typeof(T).Name + " as o" + " where o." + propertyName + " like '" + propertyValue + "%'").UniqueResult());
return r;
}
}
2- by using ICriteria
and SetProjections
like below
public long RetrieveCount<T>(string propertyName, object propertyValue)
{
using (ISession session = m_SessionFactory.OpenSession())
{
// Create a criteria object with the specified criteria
ICriteria criteria = session.CreateCriteria(typeof(T));
criteria.Add(Expression.InsensitiveLike(propertyName, propertyValue))
.SetProjection(Projections.Count(propertyName));
long count = Convert.ToInt64(criteria.UniqueResult());
// Set return value
return count;
}
}
Now my question is that which one has better performance? why?