在nhibernate中执行自定义查询并映射到自定义域对象 [英] Execute a custom query in nhibernate and map to a custom domain object
本文介绍了在nhibernate中执行自定义查询并映射到自定义域对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的查询
SELECT Customer.Name, sum([Load].Profit) as Profit FROM Customer INNER JOIN [Load] ON Customer.Id = [Load].CustomerId GROUP BY Customer.Name
我需要在nhibernate中执行此查询并将其映射到我这样创建的自定义域对象
I need to execute this query in nhibernate and map it to a Custom domain object which i created as like this
public class CustomerProfit
{
public String Name;
public Decimal Profit;
}
有可能这样做吗?以及如何或是否有可能在HQL中执行此自定义查询?
Is it possible to do so ? and how , or is it possible to execute this custom query in HQL ?
推荐答案
public sealed class CustomerProfitQuery : IResultTransformer
{
public static readonly string Sql = "SELECT Customer.Name, sum([Load].Profit) as Profit FROM Customer INNER JOIN [Load] ON Customer.Id = [Load].CustomerId GROUP BY Customer.Name";
public static readonly CustomerProfitQuery Transformer = new CustomerProfitQuery();
// make it singleton
private CustomerProfitQuery()
{ }
public IList TransformList(IList collection)
{
return collection;
}
public object TransformTuple(object[] tuple, string[] aliases)
{
return new CustomerProfit
{
Name = (string)tuple[0],
Profit = (decimal)tuple[1],
};
}
}
// usage
var customerprofits = session.CreateSQLQuery(CustomerProfitQuery.Sql)
.SetResultTransformer(CustomerProfitQuery.Transformer)
.List<CustomerProfit>()
这篇关于在nhibernate中执行自定义查询并映射到自定义域对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文