NHibernate:如何使用字段名从IQuery.List()中检索数据?

时间:2010-12-08 04:01:27

标签: nhibernate

我的查询将类似于:

string query = " select a.CustomerName , b.ProductName from Customer as a join a.Product as b " 

IQuery query = applicationSession.CreateQuery(query);
IList listResult = query.List();

这将返回客户名称和产品名称。 但我想做的是:

string customerName = listResult[0]["CustomerName"];
string productName = listResult[0]["ProductName"];

有人可以帮我解决一下这个问题吗?

1 个答案:

答案 0 :(得分:4)

您可以将变换器应用于Hashtable:

string query = "select a.CustomerName as CustomerName, b.ProductName as ProductName from Customer as a join a.Product as b" 

IQuery query = applicationSession.CreateQuery(query);
var listResult = query.SetResultTransformer(NHibernate.Transform.Transformers.AliasToEntityMap).List<Hashtable>();


string customerName = (string)listResult[0]["CustomerName"];
string productName  = (string)listResult[0]["ProductName"];

请注意,我在原始查询中添加了别名,以使AliasToEntityMap正常工作。