X ++ QueryBuilder嵌套结果

时间:2016-10-27 20:45:15

标签: axapta query-builder x++

我有一个查询,它使用Sales Table和Sales Line表作为查询的数据源(感谢所有帮助过它的人!)。在“查询运行”部分中,如果销售订单附加了多个销售线,如何循环销售行的结果?

static void TestQuery(Args _args)

{

    SalesTable salesTable;
    SalesLine salesLine;
    QueryBuildRange      querybuildrange;
    utcDateTime          mutcDateTime;
    date                 mDate;

    QueryRun queryrun = new QueryRun(new Query());
    QueryBuildDataSource datasourceA = queryrun.query().addDataSource(tableNum(SalesTable));
    QueryBuildDataSource datasourceB = datasourceA.addDataSource(tableNum(SalesLine));

    mDate        = str2Date("25/09/2016", 123);
    mutcDateTime = DateTimeUtil::newDateTime(mDate,0);
    querybuildrange = datasourceA.addRange(FieldNum(SalesTable, CreatedDateTime));
    querybuildrange.value(strFmt('>%1', mutcDateTime));

    datasourceB.relations(true);  // Link on SalesId
    datasourceB.joinMode(JoinMode::ExistsJoin);

    info(datasourceA.toString());      // This is the full query

    while (queryrun.next())
    {
        salesTable = queryrun.get(tableNum(SalesTable));
        salesLine = queryrun.get(tableNum(SalesLine));
        info(salesTable.SalesId);
        info(salesline.Name);
    }
}

1 个答案:

答案 0 :(得分:2)

当您需要使用ExistsJoins时,您正在使用InnerJoin

更改此行

  

datasourceB.joinMode(JoinMode::ExistsJoin);

  

datasourceB.joinMode(JoinMode::InnerJoin);

此外,您可以使用queryRun.changed(...)方法检查记录何时发生更改。像这样:

while (queryrun.next())
{
    if (queryrun.changed(tableNum(SalesTable)))
    {
        salesTable = queryrun.get(tableNum(SalesTable));
    }

    salesLine = queryrun.get(tableNum(SalesLine));
    info(salesTable.SalesId);
    info(salesline.Name);
}