[英]Number of records in grid AX 2012
我尝试使用此代码在运行时计算网格中的行数
FormRun caller;
FormDataSource fds;
QueryRun queryRun;
int64 rows;
fds = caller.dataSource();
query = fds.query();
queryRun = new QueryRun(query);
rows = SysQuery::countTotal(queryRun); //this returns -1587322268
rows = SysQuery::countLoops(queryRun); //this returs 54057
最后一行代码最接近我需要的,因为有 54057 行,但如果我添加过滤器,它仍然返回 54057。我希望逻辑在调用方法时获取网格具有的行数。
您的查询有多个数据源。
解释您的观察结果的最佳方式是查看countTotal
和countLoops
的实现。
public client server static Integer countTotal(QueryRun _queryRun)
{
container c = SysQuery::countPrim(_queryRun.pack(false));
return conpeek(c,1);
}
public client server static Integer countLoops(QueryRun _queryRun)
{
container c = SysQuery::countPrim(_queryRun.pack(false));
return conpeek(c,2);
}
private server static container countPrim(container _queryPack)
{
...
if (countQuery.dataSourceCount() == 1)
qbds.addSelectionField(fieldnum(Common,RecId),SelectionField::Count);
countQueryRun = new QueryRun(countQuery);
while (countQueryRun.next())
{
common = countQueryRun.get(countQuery.dataSourceNo(1).table());
counter += common.RecId;
loops++;
}
return [counter,loops];
}
如果您的数据源包含一个数据源,它会添加count(RecId)
。
countTotal
返回记录数。
countLoops
返回 1。
非常快,与 SQL 允许的一样快。
如果您的数据源包含多个数据源,则不会添加count(RecId)
。
countTotal
返回 recIds 的总和(没有意义)。
countLoops
返回记录数。
如果有很多记录,则countLoops
也很慢,因为它们是一条一条地计算的。
如果您有两个数据源并且想要快速计数,那么您只能靠自己了:
fds = caller.dataSource();
queryRun = new QueryRun(fds.queryRun().query());
queryRun.query().dataSourceNo(2).joinMode(JoinMode::ExistsJoin);
queryRun.query().dataSourceNo(1).clearFields();
queryRun.query().dataSourceNo(1).addSelectionField(fieldnum(Common,RecId),SelectionField::Count);
queryRun.next();
rows = queryRun.getNo(1).RecId;
您的计数不遵守过滤器的原因是因为您使用了datasource.query()
而不是datasource.queryRun().query()
。 前者是 static 查询,后者是包含用户过滤器的动态查询。
更新,发现了一些具有更通用方法的旧代码:
static int tableCount(QueryRun _qr)
{
QueryRun qr;
Query q = new Query(_qr.query());
int dsN = _qr.query().dataSourceCount();
int ds;
for (ds = 2; ds <= dsN; ++ds)
{
if (q.dataSourceNo(ds).joinMode() == JoinMode::OuterJoin)
q.dataSourceNo(ds).enabled(false);
else if (q.dataSourceNo(ds).joinMode() == JoinMode::InnerJoin)
{
q.dataSourceNo(ds).joinMode(JoinMode::ExistsJoin);
q.dataSourceNo(ds).fields().clearFieldList();
}
}
q.dataSourceNo(1).fields().clearFieldList();
q.dataSourceNo(1).addSelectionField(fieldNum(Common,RecId), SelectionField::Count);
qr = new QueryRun(q);
qr.next();
return any2int(qr.getNo(1).RecId);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.