I have a Linq issue where the aliases I assign get ignored, leading to an incorrect SQL query.
This is the Linq:
oCrit := oMgr.Find<TPosting>
.select(
TProjections.ProjectionList
.Add( TProjections.Alias( TProjections.Prop( 'PROJID' ), 'PROJID' ) )
.Add(
TProjections.Alias(
TProjections.Add(
TProjections.Sum( 'DURATION' ),
TProjections.Condition(
Linq['ADJUSTMENT'].IsNull and Linq['STOPTIME'].IsNull,
// project is running
TProjections.Literal<TDateTime>( now ) - TLinq['STARTTIME'],
// project is not running
TProjections.Literal<integer>( 0 ) )
), 'SUM_DURATION' )
)
.Add( TProjections.Group( 'PROJID' ) )
)
.where( ( Linq['USERID'] = AUserID )
and ( Linq['STARTTIME'] >= ADate )
and ( Linq['STARTTIME'] < EndOfTheDay( ADate ) ) )
.ListValues;
And this is the SQL generated:
SELECT
A.PROJID As f0_,
(sum(A.DURATION) +
(Case When (A.ADJUSTMENT Is Null And A.STOPTIME Is Null) Then
(TIMESTAMP ''2022-10-07 15:26:13'' - A.STARTTIME) Else 0 End)) As f1_,
A.PROJID As f2_
FROM POSTINGS A
WHERE ((A.USERID = :p_0 And A.STARTTIME >= :p_1) And A.STARTTIME < p_2)
GROUP BY A.PROJID
To explain, the records contain three fields of interest, ADJUSTMENT, STOPTIME and DURATION. When ADJUSTMENT and STOPTIME are both null, I need to add now() - STARTTIME to the DURATION, then group it all by PROJID.
But the SQL generated seems to ignore the Alias() projections completely thus producing illegal SQL.
Also, why the second PROJID (f2_)?
Probably, my Linq is wrong but I can't see how and where...