NHibernate : How to retrieve the data from IQuery.List() using field name?
My Query will be something like:
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();
This will return the customer names and product names. But what I want to do is something like:
string customerName = listResu开发者_如何学运维lt[0]["CustomerName"];
string productName = listResult[0]["ProductName"];
Could anyone give me a help on how I can make this?
You could apply a transformer to 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"];
Please note that I added aliases to your original query to make the AliasToEntityMap working.
精彩评论