简体   繁体   English

NHibernate的CreateQuery()。list()输出列/投影

[英]NHibernate output columns/projections of CreateQuery().list()

I have simple HQL query: 我有简单的HQL查询:

var list = OpenSession()
              .CreateQuery("SELECT MAX(p.price) as max_price, 
                                   COUNT(p.id) as count_all 
                            FROM Order o left join o.Products p")
              .List();

I would like to output "max_price" and "count_all" columns/projections as easy as possible. 我想输出“ max_price”和“ count_all”列/投影尽可能容易。

Something like: 就像是:

Console.WriteLine(list[0]["max_price"]);
Console.WriteLine(list[0]["count_all]);

Any idea? 任何想法?

您可以将其转换为Hashtable

.SetResultTransformer(Transformers.AliasToEntityMap).List<HashTable>()[0]["max_price"];

Not sure about this but I think that you will need to create a class and project into that. 对此不确定,但我认为您将需要创建一个类并对其进行项目。 This is how I would start by approaching it 这就是我从接近它开始的方式

class UserStatistics{
 MaxPrice {get; set;}
 CountAll {get; set;}
}

var list = OpenSession()
              .CreateQuery("SELECT MAX(p.price) as max_price, 
                                   COUNT(p.id) as count_all 
                            FROM Order o left join o.Products p")
              .SetResultTransformer(NHibernate.Transform.Transformers.AliasToBean(typeof(UserStatistics)))
              .List<UserStatistics>();

then it should be a matter of 那应该是一个问题

Console.WriteLine(list[0].MaxPrice);
Console.WriteLine(list[0].CountAll);

Great Post explaining better. 很棒的帖子解释得更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM