使用 Castle ActiveRecord 进行自定义查询

2023-12-02

我试图弄清楚如何使用 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(使用前将#替换为@)

使用 Castle ActiveRecord 进行自定义查询 的相关文章

  • 计算午夜后两次之间的持续时间

    如何计算两次之间的持续时间 var start moment 17 00 HH mm var end moment 02 15 HH mm moment duration end diff start asHours outputs 14
  • 使用 Python 中的 Requests 库发送“用户代理”

    我想发送一个值 User agent 使用 Python Requests 请求网页时 我不确定是否可以将其作为标头的一部分发送 如下面的代码所示 debug verbose sys stderr user agent User agent
  • Xcode 中的 Git 提交模板

    我已经添加到全局 git 配置中 git config global commit template gitmessage 创建了 gitmessage file one line summary of changes Because re
  • Cocoa:限制鼠标在屏幕上

    我正在为 OSX 开发一个信息亭模式应用程序 在某些情况下 会连接另一个屏幕 我的应用程序在一个屏幕上全屏运行 使用 self window contentView enterFullScreenMode s withOptions NSD
  • 使用 FastAPI 解析来自 Slack 的传入 POST 请求

    我正在构建一个 FastAPI 服务器来接收 slacklash 命令发送的请求 使用下面的代码 我可以看到以下内容 token BLAHBLAH team id BLAHBLAH team domain myteam channel id

随机推荐