Ease of use LINQ (if you are using NHibernate 3.0 or later):
var products = from p in Session.Query<Product>() where
In addition, if you use HQL, you can simply select the columns you want to use T-SQL, but use Transformer to get a strongly typed object:
First create a class with narrowed columns:
public class ProductReport { public string Name { get; set; } public string Description { get; set; } public decimal Price { get; set; } public int Units { get; set; } }
Then your request:
string hql = "select p.ProductName as Name, p.ShortDesc as Description ...(snip) " + "from Product p " + "where ...some query (snip)"; IQuery query = Session.CreateQuery(hql) .SetResultTransformer(Transformers.AliasToBean<ProductReport>()); IList<ProductReport> products = query.List<ProductReport>();
Just make sure that the aliases in the request (as a name, as a description, etc.) match the property names in your class.
source share