我试图弄清楚如何使用 Castle ActiveRecord 执行自定义查询。
我能够运行返回实体的简单查询,但我真正需要的是如下所示的查询(带有自定义字段集):
选择 count(1) 作为 cnt,来自workstationevent 的数据,其中 serverdatetime >= :minDate 和 serverdatetime :threshold 的数据分组
Thanks!
在这种情况下你想要的是HqlBasedQuery
。您的查询将是一个投影,因此您将得到一个ArrayList
包含结果的元组(ArrayList 的每个元素的内容将取决于查询,但对于多个值将是object[]
).
HqlBasedQuery query = new HqlBasedQuery(typeof(WorkStationEvent),
"select count(1) as cnt, data from workstationevent where
serverdatetime >= :minDate and serverdatetime < :maxDate
and userId = 1 group by data having count(1) > :threshold");
var results =
(ArrayList)ActiveRecordMediator.ExecuteQuery(query);
foreach(object[] tuple in results)
{
int count = (int)tuple[0]; // = cnt
string data = (string)tuple[1]; // = data (assuming this is a string)
// do something here with these results
}
您可以创建匿名类型以更有意义的方式保存结果。例如:
var results = from summary in
(ArrayList)ActiveRecordMediator.ExecuteQuery(query)
select new {
Count = (int)summary[0], Data = (string)summary[1]
};
现在结果将包含具有属性的匿名类型的集合Count
and Data
。或者实际上您也可以创建自己的摘要类型并以这种方式填充它。
ActiveRecord 还具有ProjectionQuery
它的作用大致相同,但只能返回实际的映射属性,而不是像 HQL 那样返回聚合或函数。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)