2016-10-27 74 views
1

我有一個使用Sales Table和Sales Line表作爲查詢數據源的查詢(感謝所有幫助過這個的人!)。在查詢運行部分,如果銷售訂單有多個銷售行附加到銷售行,我該如何循環銷售行結果?X ++ QueryBuilder嵌套結果

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); 
    } 
} 

回答

2

當您需要使用InnerJoin時,您正在使用ExistsJoins

改變這一行

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); 
} 
+0

感謝Alex的回答,感謝幫助! –

+0

不客氣。如果它是正確的,你可以標記答案正確... –